Commit 180d435e authored by zYne's avatar zYne

Relation model rewrite, first draft

parent 84cf99fb
...@@ -261,7 +261,7 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator ...@@ -261,7 +261,7 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator
* *
* @return void * @return void
*/ */
public function setReference(Doctrine_Record $record,Doctrine_Relation $relation) public function setReference(Doctrine_Record $record, Doctrine_Relation $relation)
{ {
$this->reference = $record; $this->reference = $record;
$this->relation = $relation; $this->relation = $relation;
...@@ -607,30 +607,27 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator ...@@ -607,30 +607,27 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator
$list[] = $value; $list[] = $value;
} }
}; };
$query->from($this->table->getComponentName()."(".implode(", ",$this->table->getPrimaryKeys()).")"); $query->from($this->table->getComponentName() . '(' . implode(", ",$this->table->getPrimaryKeys()) . ')');
$query->where($this->table->getComponentName().".id IN (".substr(str_repeat("?, ", count($list)),0,-2).")"); $query->where($this->table->getComponentName() . '.id IN (' . substr(str_repeat("?, ", count($list)),0,-2) . ')');
return $query; return $query;
} }
$rel = $this->table->getRelation($name); $rel = $this->table->getRelation($name);
$table = $rel->getTable();
$foreign = $rel->getForeign();
$local = $rel->getLocal();
if ($rel instanceof Doctrine_Relation_LocalKey || $rel instanceof Doctrine_Relation_ForeignKey) { if ($rel instanceof Doctrine_Relation_LocalKey || $rel instanceof Doctrine_Relation_ForeignKey) {
foreach ($this->data as $record) { foreach ($this->data as $record) {
$list[] = $record[$local]; $list[] = $record[$rel->getLocal()];
}; }
} else { } else {
foreach ($this->data as $record) { foreach ($this->data as $record) {
$value = $record->getIncremented(); $value = $record->getIncremented();
if ($value !== null) { if ($value !== null) {
$list[] = $value; $list[] = $value;
} }
}; }
} }
$this->table->getRelation($name);
$dql = $rel->getRelationDql(count($list), 'collection'); $dql = $rel->getRelationDql(count($list), 'collection');
$coll = $query->query($dql, $list); $coll = $query->query($dql, $list);
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
* and is licensed under the LGPL. For more information, see * and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.com>. * <http://www.phpdoctrine.com>.
*/ */
Doctrine::autoload('Doctrine_Configurable');
/** /**
* Doctrine_Connection * Doctrine_Connection
* *
...@@ -481,8 +482,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun ...@@ -481,8 +482,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun
. 'VALUES (' . substr(str_repeat('?, ', count($values)), 0, -2) . ')'; . 'VALUES (' . substr(str_repeat('?, ', count($values)), 0, -2) . ')';
// prepare and execute the statement // prepare and execute the statement
$stmt = $this->dbh->prepare($query); $this->execute($query, array_values($values));
$stmt->execute(array_values($values));
return true; return true;
} }
...@@ -684,7 +684,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun ...@@ -684,7 +684,7 @@ abstract class Doctrine_Connection extends Doctrine_Configurable implements Coun
} }
} catch(Doctrine_Adapter_Exception $e) { } catch(Doctrine_Adapter_Exception $e) {
} catch(PDOException $e) { } } catch(PDOException $e) { }
print Doctrine_Lib::formatSql($query);
$this->rethrowException($e); $this->rethrowException($e);
} }
/** /**
......
...@@ -89,6 +89,20 @@ class Doctrine_Connection_Pgsql extends Doctrine_Connection_Common ...@@ -89,6 +89,20 @@ class Doctrine_Connection_Pgsql extends Doctrine_Connection_Common
$query = 'SET NAMES '.$this->dbh->quote($charset); $query = 'SET NAMES '.$this->dbh->quote($charset);
$this->exec($query); $this->exec($query);
} }
/**
* convertBoolean
* some drivers need the boolean values to be converted into integers
* when using DQL API
*
* This method takes care of that conversion
*
* @param array $item
* @return void
*/
public function convertBooleans(array $items)
{
return $items;
}
/** /**
* Changes a query string for various DBMS specific reasons * Changes a query string for various DBMS specific reasons
* *
......
...@@ -615,7 +615,7 @@ class Doctrine_Export extends Doctrine_Connection_Module ...@@ -615,7 +615,7 @@ class Doctrine_Export extends Doctrine_Connection_Module
* The onDelete and onUpdate keys accept the following values: * The onDelete and onUpdate keys accept the following values:
* *
* CASCADE: Delete or update the row from the parent table and automatically delete or * CASCADE: Delete or update the row from the parent table and automatically delete or
* update the matching rows in the child table. Both ON DELETE CASCADE and ON UPDATE CASCADE are supported. * update the matching rows in the child table. Both ON DELETE CASCADE and ON UPDATE CASCADE are supported.
* Between two tables, you should not define several ON UPDATE CASCADE clauses that act on the same column * Between two tables, you should not define several ON UPDATE CASCADE clauses that act on the same column
* in the parent table or in the child table. * in the parent table or in the child table.
* *
...@@ -624,7 +624,7 @@ class Doctrine_Export extends Doctrine_Connection_Module ...@@ -624,7 +624,7 @@ class Doctrine_Export extends Doctrine_Connection_Module
* specified. Both ON DELETE SET NULL and ON UPDATE SET NULL clauses are supported. * specified. Both ON DELETE SET NULL and ON UPDATE SET NULL clauses are supported.
* *
* NO ACTION: In standard SQL, NO ACTION means no action in the sense that an attempt to delete or update a primary * NO ACTION: In standard SQL, NO ACTION means no action in the sense that an attempt to delete or update a primary
* key value is not allowed to proceed if there is a related foreign key value in the referenced table. * key value is not allowed to proceed if there is a related foreign key value in the referenced table.
* *
* RESTRICT: Rejects the delete or update operation for the parent table. NO ACTION and RESTRICT are the same as * RESTRICT: Rejects the delete or update operation for the parent table. NO ACTION and RESTRICT are the same as
* omitting the ON DELETE or ON UPDATE clause. * omitting the ON DELETE or ON UPDATE clause.
...@@ -634,9 +634,9 @@ class Doctrine_Export extends Doctrine_Connection_Module ...@@ -634,9 +634,9 @@ class Doctrine_Export extends Doctrine_Connection_Module
* @return string DBMS specific SQL code portion needed to set the FOREIGN KEY constraint * @return string DBMS specific SQL code portion needed to set the FOREIGN KEY constraint
* of a field declaration. * of a field declaration.
*/ */
public function getForeignKeyDeclaration($definition) public function getForeignKeyDeclaration(array $definition)
{ {
$sql = $this->getForeignKeyBaseDeclaration(); $sql = $this->getForeignKeyBaseDeclaration($definition);
if (isset($definition['deferred'])) { if (isset($definition['deferred'])) {
$sql .= ' ' . $this->getForeignKeyDeferredDeclaration(); $sql .= ' ' . $this->getForeignKeyDeferredDeclaration();
...@@ -673,15 +673,19 @@ class Doctrine_Export extends Doctrine_Connection_Module ...@@ -673,15 +673,19 @@ class Doctrine_Export extends Doctrine_Connection_Module
} }
/** /**
* getForeignKeyBaseDeclaration * getForeignKeyBaseDeclaration
* Obtain DBMS specific SQL code portion needed to set the FOREIGN KEY constraint
* of a field declaration to be used in statements like CREATE TABLE.
* *
* @param array $definition
* @return string * @return string
*/ */
public function getForeignKeyBaseDeclaration() public function getForeignKeyBaseDeclaration(array $definition)
{ {
$sql = ''; $sql = '';
if (isset($definition['name'])) { if (isset($definition['name'])) {
$sql .= 'CONSTRAINT ' . $definition['name']; $sql .= 'CONSTRAINT ' . $definition['name'] . ' ';
} }
$sql .= 'FOREIGN KEY ';
if ( ! isset($definition['local'])) { if ( ! isset($definition['local'])) {
throw new Doctrine_Export_Exception('Local reference field missing from definition.'); throw new Doctrine_Export_Exception('Local reference field missing from definition.');
} }
......
...@@ -86,7 +86,6 @@ class Doctrine_Export_Mysql extends Doctrine_Export ...@@ -86,7 +86,6 @@ class Doctrine_Export_Mysql extends Doctrine_Export
* 'comment' => 'Foo', * 'comment' => 'Foo',
* 'charset' => 'utf8', * 'charset' => 'utf8',
* 'collate' => 'utf8_unicode_ci', * 'collate' => 'utf8_unicode_ci',
* 'collate' => 'utf8_unicode_ci',
* 'type' => 'innodb', * 'type' => 'innodb',
* ); * );
* *
...@@ -106,6 +105,13 @@ class Doctrine_Export_Mysql extends Doctrine_Export ...@@ -106,6 +105,13 @@ class Doctrine_Export_Mysql extends Doctrine_Export
$queryFields .= ', ' . $this->getIndexDeclaration($index, $definition); $queryFields .= ', ' . $this->getIndexDeclaration($index, $definition);
} }
} }
if (isset($options['foreignKeys']) && ! empty($options['foreignKeys'])) {
foreach($options['foreignKeys'] as $definition) {
$queryFields .= ', ' . $this->getForeignKeyDeclaration($definition);
}
}
if (isset($options['primary']) && ! empty($options['primary'])) { if (isset($options['primary']) && ! empty($options['primary'])) {
$queryFields .= ', PRIMARY KEY(' . implode(', ', array_values($options['primary'])) . ')'; $queryFields .= ', PRIMARY KEY(' . implode(', ', array_values($options['primary'])) . ')';
} }
...@@ -333,13 +339,17 @@ class Doctrine_Export_Mysql extends Doctrine_Export ...@@ -333,13 +339,17 @@ class Doctrine_Export_Mysql extends Doctrine_Export
$sequenceName = $this->conn->quoteIdentifier($this->conn->getSequenceName($sequenceName), true); $sequenceName = $this->conn->quoteIdentifier($this->conn->getSequenceName($sequenceName), true);
$seqcolName = $this->conn->quoteIdentifier($this->conn->getAttribute(Doctrine::ATTR_SEQCOL_NAME), true); $seqcolName = $this->conn->quoteIdentifier($this->conn->getAttribute(Doctrine::ATTR_SEQCOL_NAME), true);
$query = 'CREATE TABLE ' . $sequenceName try {
. ' (' . $seqcolName . ' INT NOT NULL AUTO_INCREMENT, PRIMARY KEY (' $query = 'CREATE TABLE ' . $sequenceName
. $seqcolName . '))' . ' (' . $seqcolName . ' INT NOT NULL AUTO_INCREMENT, PRIMARY KEY ('
. strlen($this->conn->default_table_type) ? ' TYPE = ' . $seqcolName . '))'
. $this->conn->default_table_type : ''; . strlen($this->conn->default_table_type) ? ' TYPE = '
. $this->conn->default_table_type : '';
$res = $this->conn->exec($query);
$res = $this->conn->exec($query);
} catch(Doctrine_Connection_Exception $e) {
throw new Doctrine_Export_Exception('could not create sequence table');
}
if ($start == 1) if ($start == 1)
return true; return true;
...@@ -352,11 +362,11 @@ class Doctrine_Export_Mysql extends Doctrine_Export ...@@ -352,11 +362,11 @@ class Doctrine_Export_Mysql extends Doctrine_Export
// Handle error // Handle error
try { try {
$result = $this->conn->exec('DROP TABLE ' . $sequenceName); $result = $this->conn->exec('DROP TABLE ' . $sequenceName);
} catch(Exception $e) { } catch(Doctrine_Connection_Exception $e) {
throw new Doctrine_Export_Exception('could not drop inconsistent sequence table'); throw new Doctrine_Export_Exception('could not drop inconsistent sequence table');
} }
throw new Doctrine_Export_Exception('could not create sequence table');
} }
/** /**
* Get the stucture of a field into an array * Get the stucture of a field into an array
...@@ -462,9 +472,12 @@ class Doctrine_Export_Mysql extends Doctrine_Export ...@@ -462,9 +472,12 @@ class Doctrine_Export_Mysql extends Doctrine_Export
} }
} }
if ( ! isset($definition['fields']) || ! is_array($definition['fields'])) { if ( ! isset($definition['fields'])) {
throw new Doctrine_Export_Exception('No index columns given.'); throw new Doctrine_Export_Exception('No index columns given.');
} }
if ( ! is_array($definition['fields'])) {
$definition['fields'] = array($definition['fields']);
}
$query = $type . 'INDEX ' . $name; $query = $type . 'INDEX ' . $name;
......
...@@ -54,7 +54,7 @@ class Doctrine_Lib ...@@ -54,7 +54,7 @@ class Doctrine_Lib
case Doctrine_Record::STATE_TCLEAN: case Doctrine_Record::STATE_TCLEAN:
return "transient clean"; return "transient clean";
break; break;
}; }
} }
/** /**
* returns a string representation of Doctrine_Record object * returns a string representation of Doctrine_Record object
...@@ -89,7 +89,7 @@ class Doctrine_Lib ...@@ -89,7 +89,7 @@ class Doctrine_Lib
case Doctrine_Transaction::STATE_ACTIVE: case Doctrine_Transaction::STATE_ACTIVE:
return "active"; return "active";
break; break;
}; }
} }
/** /**
* returns a string representation of Doctrine_Connection object * returns a string representation of Doctrine_Connection object
...@@ -98,28 +98,12 @@ class Doctrine_Lib ...@@ -98,28 +98,12 @@ class Doctrine_Lib
*/ */
public static function getConnectionAsString(Doctrine_Connection $connection) public static function getConnectionAsString(Doctrine_Connection $connection)
{ {
$r[] = "<pre>"; $r[] = '<pre>';
$r[] = "Doctrine_Connection object"; $r[] = 'Doctrine_Connection object';
$r[] = "State : ".Doctrine_Lib::getConnectionStateAsString($connection->getTransaction()->getState()); $r[] = 'State : ' . Doctrine_Lib::getConnectionStateAsString($connection->transaction->getState());
$r[] = "Open Transactions : ".$connection->getTransaction()->getTransactionLevel(); $r[] = 'Open Transactions : ' . $connection->transaction->getTransactionLevel();
$r[] = "Table in memory : ".$connection->count(); $r[] = 'Table in memory : ' . $connection->count();
$r[] = 'Driver name : ' . $connection->getDbh()->getAttribute(Doctrine::ATTR_DRIVER_NAME);
$queries = false;
if ($connection->getDBH() instanceof Doctrine_Db) {
$handler = "Doctrine Database Handler";
$queries = count($connection->getDBH()->getQueries());
$sum = array_sum($connection->getDBH()->getExecTimes());
} elseif ($connection->getDBH() instanceof PDO) {
$handler = "PHP Native PDO Driver";
} else {
$handler = "Unknown Database Handler";
}
$r[] = "DB Handler : ".$handler;
if ($queries) {
$r[] = "Executed Queries : ".$queries;
$r[] = "Sum of Exec Times : ".$sum;
}
$r[] = "</pre>"; $r[] = "</pre>";
return implode("\n",$r)."<br>"; return implode("\n",$r)."<br>";
......
...@@ -231,7 +231,7 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera ...@@ -231,7 +231,7 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera
$this->connections[$name] = new Doctrine_Connection_Mock($this, $adapter); $this->connections[$name] = new Doctrine_Connection_Mock($this, $adapter);
break; break;
default: default:
throw new Doctrine_Manager_Exception('Unknown connection driver '. $adapter->getAttribute(PDO::ATTR_DRIVER_NAME)); throw new Doctrine_Manager_Exception('Unknown connection driver '. $adapter->getAttribute(Doctrine::ATTR_DRIVER_NAME));
}; };
if ($setCurrent) { if ($setCurrent) {
......
...@@ -775,7 +775,8 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite ...@@ -775,7 +775,8 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
if ( ! isset($this->references[$name])) { if ( ! isset($this->references[$name])) {
$this->loadReference($name); $this->loadReference($name);
} }
} catch(Doctrine_Table_Exception $e) { } catch(Doctrine_Table_Exception $e) {
print $e;
throw new Doctrine_Record_Exception("Unknown property / related component '$name'."); throw new Doctrine_Record_Exception("Unknown property / related component '$name'.");
} }
...@@ -1525,6 +1526,17 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite ...@@ -1525,6 +1526,17 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
$this->_table->setOption($name, $value); $this->_table->setOption($name, $value);
} }
} }
/**
* index
* defines a foreignKey
*
* @param array $definition the definition array
* @return void
*/
public function foreignKey(array $definition = array())
{
return $this->_table->addForeignKey($definition);
}
/** /**
* index * index
* defines or retrieves an index * defines or retrieves an index
...@@ -1533,7 +1545,6 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite ...@@ -1533,7 +1545,6 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
* *
* @param string $name the name of the index * @param string $name the name of the index
* @param array $definition the definition array * @param array $definition the definition array
* @param array $options an array of options
* @return mixed * @return mixed
*/ */
public function index($name, array $definition = array()) public function index($name, array $definition = array())
......
...@@ -53,41 +53,66 @@ abstract class Doctrine_Relation ...@@ -53,41 +53,66 @@ abstract class Doctrine_Relation
*/ */
const MANY_COMPOSITE = 3; const MANY_COMPOSITE = 3;
const ONE = 0;
const MANY = 1;
protected $definition = array('alias' => true,
'foreign' => true,
'local' => true,
'table' => true,
);
/** /**
* @var Doctrine_Table $table foreign factory * constructor
*/ *
protected $table; * @param array $definition an associative array with the following structure:
/** * name related class name
* @var string $local local field *
*/ * local the local field(s)
protected $local; *
/** * foreign the foreign reference field(s)
* @var string $foreign foreign field *
*/ * table the foreign table object
protected $foreign; *
/** * assocTable the association table object (if any)
* @var integer $type bind type *
*/ * onDelete referential delete action
protected $type; *
/** * onUpdate referential update action
* @var string $alias relation alias *
*/ * deferred deferred constraint checking
protected $alias; *
* alias relation alias
/** *
* @param Doctrine_Table $table * type the relation type, either Doctrine_Relation::ONE or Doctrine_Relation::MANY
* @param string $local *
* @param string $foreign * The onDelete and onUpdate keys accept the following values:
* @param integer $type *
* @param string $alias * CASCADE: Delete or update the row from the parent table and automatically delete or
* update the matching rows in the child table. Both ON DELETE CASCADE and ON UPDATE CASCADE are supported.
* Between two tables, you should not define several ON UPDATE CASCADE clauses that act on the same column
* in the parent table or in the child table.
*
* SET NULL: Delete or update the row from the parent table and set the foreign key column or columns in the
* child table to NULL. This is valid only if the foreign key columns do not have the NOT NULL qualifier
* specified. Both ON DELETE SET NULL and ON UPDATE SET NULL clauses are supported.
*
* NO ACTION: In standard SQL, NO ACTION means no action in the sense that an attempt to delete or update a primary
* key value is not allowed to proceed if there is a related foreign key value in the referenced table.
*
* RESTRICT: Rejects the delete or update operation for the parent table. NO ACTION and RESTRICT are the same as
* omitting the ON DELETE or ON UPDATE clause.
*
* SET DEFAULT
*/ */
public function __construct(Doctrine_Table $table, $local, $foreign, $type, $alias) public function __construct(array $definition)
{ {
$this->table = $table; foreach (array_keys($this->definition) as $key) {
$this->local = $local; if ( ! isset($definition[$key])) {
$this->foreign = $foreign; throw new Doctrine_Exception($key . ' is required!');
$this->type = $type; }
$this->alias = $alias; }
$this->definition = $definition;
} }
/** /**
* getAlias * getAlias
...@@ -97,7 +122,7 @@ abstract class Doctrine_Relation ...@@ -97,7 +122,7 @@ abstract class Doctrine_Relation
*/ */
final public function getAlias() final public function getAlias()
{ {
return $this->alias; return $this->definition['alias'];
} }
/** /**
* getType * getType
...@@ -108,7 +133,7 @@ abstract class Doctrine_Relation ...@@ -108,7 +133,7 @@ abstract class Doctrine_Relation
*/ */
final public function getType() final public function getType()
{ {
return $this->type; return $this->definition['type'];
} }
/** /**
* getTable * getTable
...@@ -118,7 +143,7 @@ abstract class Doctrine_Relation ...@@ -118,7 +143,7 @@ abstract class Doctrine_Relation
*/ */
final public function getTable() final public function getTable()
{ {
return $this->table; return $this->definition['table'];
} }
/** /**
* getLocal * getLocal
...@@ -128,7 +153,7 @@ abstract class Doctrine_Relation ...@@ -128,7 +153,7 @@ abstract class Doctrine_Relation
*/ */
final public function getLocal() final public function getLocal()
{ {
return $this->local; return $this->definition['local'];
} }
/** /**
* getForeign * getForeign
...@@ -139,7 +164,7 @@ abstract class Doctrine_Relation ...@@ -139,7 +164,7 @@ abstract class Doctrine_Relation
*/ */
final public function getForeign() final public function getForeign()
{ {
return $this->foreign; return $this->definition['foreign'];
} }
/** /**
* isComposite * isComposite
...@@ -149,8 +174,8 @@ abstract class Doctrine_Relation ...@@ -149,8 +174,8 @@ abstract class Doctrine_Relation
*/ */
final public function isComposite() final public function isComposite()
{ {
return ($this->type == Doctrine_Relation::ONE_COMPOSITE || return ($this->definition['type'] == Doctrine_Relation::ONE_COMPOSITE ||
$this->type == Doctrine_Relation::MANY_COMPOSITE); $this->definition['type'] == Doctrine_Relation::MANY_COMPOSITE);
} }
/** /**
* isOneToOne * isOneToOne
...@@ -160,8 +185,8 @@ abstract class Doctrine_Relation ...@@ -160,8 +185,8 @@ abstract class Doctrine_Relation
*/ */
final public function isOneToOne() final public function isOneToOne()
{ {
return ($this->type == Doctrine_Relation::ONE_AGGREGATE || return ($this->definition['type'] == Doctrine_Relation::ONE_AGGREGATE ||
$this->type == Doctrine_Relation::ONE_COMPOSITE); $this->definition['type'] == Doctrine_Relation::ONE_COMPOSITE);
} }
/** /**
* getRelationDql * getRelationDql
...@@ -171,8 +196,10 @@ abstract class Doctrine_Relation ...@@ -171,8 +196,10 @@ abstract class Doctrine_Relation
*/ */
public function getRelationDql($count) public function getRelationDql($count)
{ {
$dql = 'FROM ' . $this->table->getComponentName() $component = $this->definition['table']->getComponentName();
. ' WHERE ' . $this->table->getComponentName() . '.' . $this->foreign
$dql = 'FROM ' . $component
. ' WHERE ' . $component . '.' . $this->definition['foreign']
. ' IN (' . substr(str_repeat('?, ', $count), 0, -2) . ')'; . ' IN (' . substr(str_repeat('?, ', $count), 0, -2) . ')';
return $dql; return $dql;
...@@ -278,12 +305,12 @@ abstract class Doctrine_Relation ...@@ -278,12 +305,12 @@ abstract class Doctrine_Relation
public function __toString() public function __toString()
{ {
$r[] = "<pre>"; $r[] = "<pre>";
$r[] = "Class : ".get_class($this); foreach ($this->definition as $k => $v) {
$r[] = "Component : ".$this->table->getComponentName(); if(is_object($v)) {
$r[] = "Table : ".$this->table->getTableName(); $v = 'Object(' . get_class($v) . ')';
$r[] = "Local key : ".$this->local; }
$r[] = "Foreign key : ".$this->foreign; $r[] = $k . ' : ' . $v;
$r[] = "Type : ".$this->type; }
$r[] = "</pre>"; $r[] = "</pre>";
return implode("\n", $r); return implode("\n", $r);
} }
......
...@@ -35,30 +35,12 @@ Doctrine::autoload('Doctrine_Relation'); ...@@ -35,30 +35,12 @@ Doctrine::autoload('Doctrine_Relation');
*/ */
class Doctrine_Relation_Association extends Doctrine_Relation class Doctrine_Relation_Association extends Doctrine_Relation
{ {
/**
* @var Doctrine_Table $associationTable
*/
protected $associationTable;
/**
* the constructor
* @param Doctrine_Table $table foreign factory object
* @param Doctrine_Table $associationTable factory which handles the association
* @param string $local local field name
* @param string $foreign foreign field name
* @param integer $type type of relation
* @see Doctrine_Table constants
*/
public function __construct(Doctrine_Table $table, Doctrine_Table $associationTable, $local, $foreign, $type, $alias)
{
parent::__construct($table, $local, $foreign, $type, $alias);
$this->associationTable = $associationTable;
}
/** /**
* @return Doctrine_Table * @return Doctrine_Table
*/ */
public function getAssociationFactory() public function getAssociationFactory()
{ {
return $this->associationTable; return $this->definition['assocTable'];
} }
/** /**
* processDiff * processDiff
...@@ -106,23 +88,26 @@ class Doctrine_Relation_Association extends Doctrine_Relation ...@@ -106,23 +88,26 @@ class Doctrine_Relation_Association extends Doctrine_Relation
*/ */
public function getRelationDql($count, $context = 'record') public function getRelationDql($count, $context = 'record')
{ {
$component = $this->definition['assocTable']->getComponentName();
switch ($context) { switch ($context) {
case "record": case "record":
$sub = 'SQL:SELECT ' . $this->foreign. $sub = 'SQL:SELECT ' . $this->definition['foreign'].
' FROM ' . $this->associationTable->getTableName(). ' FROM ' . $this->definition['assocTable']->getTableName().
' WHERE ' . $this->local. ' WHERE ' . $this->definition['local'] .
' IN (' . substr(str_repeat("?, ", $count),0,-2) . ' IN (' . substr(str_repeat("?, ", $count),0,-2) .
')'; ')';
$dql = "FROM ".$this->table->getComponentName(); $dql = 'FROM ' . $this->definition['table']->getComponentName();
$dql .= ".".$this->associationTable->getComponentName(); $dql .= '.' . $component;
$dql .= " WHERE ".$this->table->getComponentName().".".$this->table->getIdentifier()." IN ($sub)"; $dql .= ' WHERE ' . $this->definition['table']->getComponentName()
. '.' . $this->definition['table']->getIdentifier() . ' IN (' . $sub . ')';
break; break;
case "collection": case "collection":
$sub = substr(str_repeat("?, ", $count),0,-2); $sub = substr(str_repeat("?, ", $count),0,-2);
$dql = "FROM ".$this->associationTable->getComponentName().".".$this->table->getComponentName(); $dql = 'FROM ' . $component . '.' . $this->definition['table']->getComponentName();
$dql .= " WHERE ".$this->associationTable->getComponentName().".".$this->local." IN ($sub)"; $dql .= ' WHERE ' . $component . '.' . $this->definition['local'] . ' IN (' . $sub . ')';
}; break;
}
return $dql; return $dql;
} }
...@@ -138,7 +123,7 @@ class Doctrine_Relation_Association extends Doctrine_Relation ...@@ -138,7 +123,7 @@ class Doctrine_Relation_Association extends Doctrine_Relation
{ {
$id = $record->getIncremented(); $id = $record->getIncremented();
if (empty($id)) { if (empty($id)) {
$coll = new Doctrine_Collection($this->table); $coll = new Doctrine_Collection($this->definition['table']);
} else { } else {
$coll = Doctrine_Query::create()->parseQuery($this->getRelationDql(1))->execute(array($id)); $coll = Doctrine_Query::create()->parseQuery($this->getRelationDql(1))->execute(array($id));
} }
......
...@@ -42,24 +42,31 @@ class Doctrine_Relation_Association_Self extends Doctrine_Relation_Association ...@@ -42,24 +42,31 @@ class Doctrine_Relation_Association_Self extends Doctrine_Relation_Association
{ {
switch ($context) { switch ($context) {
case 'record': case 'record':
$sub = 'SELECT '.$this->foreign. $sub = 'SELECT '.$this->definition['foreign']
' FROM '.$this->associationTable->getTableName(). . ' FROM '.$this->definition['assocTable']->getTableName()
' WHERE '.$this->local. . ' WHERE '.$this->definition['local']
' = ?'; . ' = ?';
$sub2 = 'SELECT '.$this->local.
' FROM '.$this->associationTable->getTableName().
' WHERE '.$this->foreign.
' = ?';
$dql = 'FROM ' . $this->table->getComponentName(); $sub2 = 'SELECT '.$this->definition['local']
$dql .= '.' . $this->associationTable->getComponentName(); . ' FROM '.$this->definition['assocTable']->getTableName()
$dql .= ' WHERE ' . $this->table->getComponentName() . '.' . $this->table->getIdentifier() . ' IN (' . $sub . ')'; . ' WHERE '.$this->definition['foreign']
$dql .= ' || ' . $this->table->getComponentName() . '.' . $this->table->getIdentifier() . ' IN (' . $sub2 . ')'; . ' = ?';
$dql = 'FROM ' . $this->definition['table']->getComponentName()
. '.' . $this->definition['assocTable']->getComponentName()
. ' WHERE ' . $this->definition['table']->getComponentName()
. '.' . $this->definition['table']->getIdentifier()
. ' IN (' . $sub . ')'
. ' || ' . $this->definition['table']->getComponentName()
. '.' . $this->definition['table']->getIdentifier()
. ' IN (' . $sub2 . ')';
break; break;
case 'collection': case 'collection':
$sub = substr(str_repeat('?, ', $count),0,-2); $sub = substr(str_repeat('?, ', $count),0,-2);
$dql = 'FROM '.$this->associationTable->getComponentName().'.'.$this->table->getComponentName(); $dql = 'FROM '.$this->definition['assocTable']->getComponentName()
$dql .= ' WHERE '.$this->associationTable->getComponentName().'.'.$this->local.' IN ('.$sub.')'; . '.' . $this->definition['table']->getComponentName()
. ' WHERE '.$this->definition['assocTable']->getComponentName()
. '.' . $this->definition['local'] . ' IN (' . $sub . ')';
}; };
return $dql; return $dql;
......
...@@ -37,6 +37,7 @@ class Doctrine_Relation_ForeignKey extends Doctrine_Relation ...@@ -37,6 +37,7 @@ class Doctrine_Relation_ForeignKey extends Doctrine_Relation
* processDiff * processDiff
* *
* @param Doctrine_Record $record * @param Doctrine_Record $record
* @return void
*/ */
public function processDiff(Doctrine_Record $record) public function processDiff(Doctrine_Record $record)
{ {
...@@ -75,29 +76,29 @@ class Doctrine_Relation_ForeignKey extends Doctrine_Relation ...@@ -75,29 +76,29 @@ class Doctrine_Relation_ForeignKey extends Doctrine_Relation
*/ */
public function fetchRelatedFor(Doctrine_Record $record) public function fetchRelatedFor(Doctrine_Record $record)
{ {
$id = $record->get($this->local); $id = $record->get($this->definition['local']);
if ($this->isOneToOne()) { if ($this->isOneToOne()) {
if (empty($id)) { if (empty($id)) {
$related = $this->table->create(); $related = $this->definition['table']->create();
} else { } else {
$dql = 'FROM ' . $this->table->getComponentName() $dql = 'FROM ' . $this->definition['table']->getComponentName()
. ' WHERE ' . $this->table->getComponentName() . ' WHERE ' . $this->definition['table']->getComponentName()
. '.' . $this->foreign . ' = ?'; . '.' . $this->definition['foreign'] . ' = ?';
$coll = $this->table->getConnection()->query($dql, array($id)); $coll = $this->definition['table']->getConnection()->query($dql, array($id));
$related = $coll[0]; $related = $coll[0];
} }
$related->set($this->foreign, $record, false); $related->set($this->definition['foreign'], $record, false);
} else { } else {
if (empty($id)) { if (empty($id)) {
$related = new Doctrine_Collection($this->table); $related = new Doctrine_Collection($this->definition['table']);
} else { } else {
$query = $this->getRelationDql(1); $query = $this->getRelationDql(1);
$related = $this->table->getConnection()->query($query, array($id)); $related = $this->definition['table']->getConnection()->query($query, array($id));
} }
$related->setReference($record, $this); $related->setReference($record, $this);
} }
......
...@@ -58,17 +58,17 @@ class Doctrine_Relation_LocalKey extends Doctrine_Relation ...@@ -58,17 +58,17 @@ class Doctrine_Relation_LocalKey extends Doctrine_Relation
*/ */
public function fetchRelatedFor(Doctrine_Record $record) public function fetchRelatedFor(Doctrine_Record $record)
{ {
$id = $record->get($this->local); $id = $record->get($this->definition['local']);
if (empty($id)) { if (empty($id)) {
$related = $this->table->create(); $related = $this->definition['table']->create();
} else { } else {
if ( ! ($related = $this->table->find($id))) { if ( ! ($related = $this->definition['table']->find($id))) {
$related = $this->table->create(); $related = $this->definition['table']->create();
} }
} }
$record->set($this->local, $related, false); $record->set($this->definition['local'], $related, false);
return $related; return $related;
} }
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment