diff --git a/Doctrine/Association.php b/Doctrine/Association.php index 130a5ab40..2dada81e5 100644 --- a/Doctrine/Association.php +++ b/Doctrine/Association.php @@ -1,4 +1,23 @@ . + */ /** * Doctrine_Association this class takes care of association mapping * (= many-to-many relationships, where the relationship is handled with an additional relational table @@ -13,7 +32,7 @@ class Doctrine_Association extends Doctrine_Relation { /** * @var Doctrine_Table $associationTable */ - private $associationTable; + protected $associationTable; /** * the constructor * @param Doctrine_Table $table foreign factory object @@ -48,7 +67,7 @@ class Doctrine_Association extends Doctrine_Relation { " IN (".substr(str_repeat("?, ", $count),0,-2).")"; $dql = "FROM ".$this->table->getComponentName(); - $dql .= ":".$this->associationTable->getComponentName(); + $dql .= ".".$this->associationTable->getComponentName(); $dql .= " WHERE ".$this->table->getComponentName().".".$this->table->getIdentifier()." IN ($sub)"; break; case "collection": diff --git a/Doctrine/Query.php b/Doctrine/Query.php index 4a49a5d08..d867a4150 100644 --- a/Doctrine/Query.php +++ b/Doctrine/Query.php @@ -712,8 +712,8 @@ class Doctrine_Query extends Doctrine_Hydrate implements Countable { if( ! $loadFields) { $this->subqueryAliases[] = $assocTableName; } - $this->parts["join"][$tname][$assocTableName] = $join.$assocTableName." ON ".$tname.".id = ".$assocTableName.".".$fk->getLocal(); - $this->parts["join"][$tname][$tname2] = $join.$aliasString." ON ".$tname2.".id = ".$assocTableName.".".$fk->getForeign(); + $this->parts["join"][$tname][$assocTableName] = $join.$assocTableName." ON ".$tname.".".$table->getIdentifier()." = ".$assocTableName.".".$fk->getLocal(); + $this->parts["join"][$tname][$tname2] = $join.$aliasString." ON ".$tname2.".".$table->getIdentifier()." = ".$assocTableName.".".$fk->getForeign(); } $this->joins[$tname2] = $prevTable; diff --git a/Doctrine/RawSql.php b/Doctrine/RawSql.php index df2313833..c59b4685b 100644 --- a/Doctrine/RawSql.php +++ b/Doctrine/RawSql.php @@ -123,8 +123,6 @@ class Doctrine_RawSql extends Doctrine_Hydrate { * @return string */ public function getQuery() { - - foreach($this->fields as $field) { $e = explode(".", $field); if( ! isset($e[1])) diff --git a/Doctrine/Record.php b/Doctrine/Record.php index 8760af8e9..11bb33503 100644 --- a/Doctrine/Record.php +++ b/Doctrine/Record.php @@ -1201,6 +1201,37 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite $id = $this->get($local); $coll = $graph->query($query,array($id)); $coll->setReference($this, $fk); + } elseif($fk instanceof Doctrine_Association_Self) { + $id = $this->getIncremented(); + + $q = new Doctrine_RawSql(); + + $assocTable = $fk->getAssociationFactory()->getTableName(); + $tableName = $this->getTable()->getTableName(); + $identifier = $this->getTable()->getIdentifier(); + + $sub = "SELECT ".$fk->getForeign(). + " FROM ".$assocTable. + " WHERE ".$fk->getLocal(). + " = ?"; + + $sub2 = "SELECT ".$fk->getLocal(). + " FROM ".$assocTable. + " WHERE ".$fk->getForeign(). + " = ?"; + + $q->select('{'.$tableName.'.*}, {'.$assocTable.'.*}') + ->from($tableName.' INNER JOIN '.$assocTable.' ON '. + $tableName.'.'.$identifier.' = '.$assocTable.'.'.$fk->getLocal().' OR '. + $tableName.'.'.$identifier.' = '.$assocTable.'.'.$fk->getForeign() + ) + ->where($tableName.'.'.$identifier.' IN ('.$sub.') OR '. + $tableName.'.'.$identifier.' IN ('.$sub2.')' + ); + $q->addComponent($tableName, $this->table->getComponentName()); + $q->addComponent($assocTable, $this->table->getComponentName().'.'.$fk->getAssociationFactory()->getComponentName()); + + $coll = $q->execute(array($id, $id)); } elseif($fk instanceof Doctrine_Association) { $id = $this->getIncremented(); $coll = $graph->query($query, array($id)); diff --git a/Doctrine/Table.php b/Doctrine/Table.php index 1b68490bd..682a350ca 100644 --- a/Doctrine/Table.php +++ b/Doctrine/Table.php @@ -622,7 +622,7 @@ class Doctrine_Table extends Doctrine_Configurable implements Countable { // SELF-REFERENCING THROUGH JOIN TABLE $this->relations[$e2[0]] = new Doctrine_ForeignKey($associationTable,$local,$fields[0],Doctrine_Relation::MANY_COMPOSITE, $e2[0]); - $relation = new Doctrine_Association($table,$associationTable,$fields[0],$fields[1], $type, $alias); + $relation = new Doctrine_Association_Self($table,$associationTable,$fields[0],$fields[1], $type, $alias); } else { // auto initialize a new one-to-one relationship for association table diff --git a/tests/QueryTestCase.php b/tests/QueryTestCase.php index 25baade1e..d5c2f241f 100644 --- a/tests/QueryTestCase.php +++ b/tests/QueryTestCase.php @@ -64,6 +64,14 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase { $this->assertEqual($array[0]['MAX(entity.id)'], 11); $this->assertEqual($array[0]['MIN(email.address)'], 'arnold@example.com'); $this->assertEqual($array[0]['COUNT(1)'], 14); + /** + $q = new Doctrine_Query(); + $q->from("User.Phonenumber(COUNT(id))")->groupby("User.id"); + $coll = $q->execute(); + print Doctrine_Lib::formatSql($q->getQuery()); + print_r($coll); + $this->assertEqual(count($coll), 8); + */ } @@ -1224,5 +1232,6 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase { //$this->assertTrue(isset($values['max'])); } + } ?> diff --git a/tests/RecordTestCase.php b/tests/RecordTestCase.php index 537e60d1f..723d36b71 100644 --- a/tests/RecordTestCase.php +++ b/tests/RecordTestCase.php @@ -7,6 +7,7 @@ class Doctrine_RecordTestCase extends Doctrine_UnitTestCase { $this->tables[] = "enumTest"; parent::prepareTables(); } + public function testJoinTableSelfReferencingInsertingData() { $e = new Entity(); $e->name = "Entity test"; @@ -66,25 +67,14 @@ class Doctrine_RecordTestCase extends Doctrine_UnitTestCase { $this->assertEqual(count($result), 6); - $q = "SELECT entity.id AS entity__id, - entity.name AS entity__name, - entity.loginname AS entity__loginname, - entity.password AS entity__password, - entity.type AS entity__type, - entity.created AS entity__created, - entity.updated AS entity__updated, - entity.email_id AS entity__email_id, - entity_reference.entity1 AS entity_reference__entity1, - entity_reference.entity2 AS entity_reference__entity2 - FROM entity - INNER JOIN entity_reference - ON entity.id = entity_reference.entity1 - WHERE entity.id IN (SELECT entity2 FROM entity_reference WHERE entity1 IN (?))"; +/** $stmt = $this->dbh->prepare($q); $stmt->execute(array(18)); $result = $stmt->fetchAll(PDO::FETCH_ASSOC); + print_r($result); + */ $this->connection->clear(); $e = $e->getTable()->find($e->id); @@ -95,7 +85,9 @@ class Doctrine_RecordTestCase extends Doctrine_UnitTestCase { $this->assertTrue($e->Entity[0] instanceof Entity); $this->assertTrue($e->Entity[1] instanceof Entity); - + + + $this->assertEqual(count($this->dbh), ($count + 1)); $this->assertEqual($e->Entity[0]->name, "Friend 1"); @@ -128,7 +120,9 @@ class Doctrine_RecordTestCase extends Doctrine_UnitTestCase { $this->assertEqual($coll->count(), 1); } -/** + + + public function testToArray() { $user = new User(); @@ -923,6 +917,6 @@ class Doctrine_RecordTestCase extends Doctrine_UnitTestCase { $user = $this->connection->getTable("User")->find(4); $this->assertTrue($user->getIterator() instanceof ArrayIterator); } - */ + } ?>