diff --git a/Doctrine/Query.php b/Doctrine/Query.php index d26ea17ec..8175681eb 100644 --- a/Doctrine/Query.php +++ b/Doctrine/Query.php @@ -41,6 +41,14 @@ class Doctrine_Query extends Doctrine_Access { * @var array $connectors component connectors */ private $connectors = array(); + /** + * @var array $tableAliases + */ + private $tableAliases = array(); + /** + * @var array $tableIndexes + */ + private $tableIndexes = array(); /** * @var array $dql DQL query string parts */ @@ -140,16 +148,18 @@ class Doctrine_Query extends Doctrine_Access { default: throw new Doctrine_Exception("Unknown fetchmode."); endswitch; - $cname = $table->getComponentName(); - $this->fetchModes[$cname] = $fetchmode; - $tablename = $table->getTableName(); + + $component = $table->getComponentName(); + $this->fetchModes[$component] = $fetchmode; + $tablename = $this->tableAliases[$component]; $count = count($this->tables); + foreach($names as $name) { if($count == 0) { $this->parts["columns"][] = $tablename.".".$name; } else { - $this->parts["columns"][] = $tablename.".".$name." AS ".$cname."__".$name; + $this->parts["columns"][] = $tablename.".".$name." AS ".$component."__".$name; } } } @@ -317,9 +327,10 @@ class Doctrine_Query extends Doctrine_Access { // get the inheritance maps $array = array(); - foreach($this->tables as $objTable): - $tname = $objTable->getTableName(); - $array[$tname][] = $objTable->getInheritanceMap(); + foreach($this->tables as $table): + $component = $table->getComponentName(); + $tableName = $this->tableAliases[$component]; + $array[$tableName][] = $table->getInheritanceMap(); endforeach; // apply inheritance maps @@ -330,7 +341,7 @@ class Doctrine_Query extends Doctrine_Access { $a = array(); foreach($maps as $map) { $b = array(); - foreach($map as $field=>$value) { + foreach($map as $field => $value) { $b[] = $tname.".$field = $value"; } if( ! empty($b)) $a[] = implode(" AND ",$b); @@ -390,6 +401,7 @@ class Doctrine_Query extends Doctrine_Access { $keys = array_keys($this->tables); + $name = $this->tables[$keys[0]]->getComponentName(); $stmt = $this->session->execute($query,$params); @@ -922,6 +934,7 @@ class Doctrine_Query extends Doctrine_Access { $tname = $table->getTableName(); $this->parts["from"][$tname] = true; + $this->tableAliases[$name] = $tname; } else { $index += strlen($e[($key - 1)]) + 1; @@ -952,7 +965,7 @@ class Doctrine_Query extends Doctrine_Access { if($fk instanceof Doctrine_ForeignKey || $fk instanceof Doctrine_LocalKey) { - + $this->parts["join"][$tname][$tname2] = $join.$tname2." ON ".$tname.".".$fk->getLocal()." = ".$tname2.".".$fk->getForeign(); } elseif($fk instanceof Doctrine_Association) { @@ -961,14 +974,23 @@ class Doctrine_Query extends Doctrine_Access { $assocTableName = $asf->getTableName(); $this->parts["join"][$tname][$assocTableName] = $join.$assocTableName." ON ".$tname.".id = ".$assocTableName.".".$fk->getLocal(); - $this->parts["join"][$tname][$tname2] = $join.$tname2." ON ".$tname2.".id = ".$assocTableName.".".$fk->getForeign(); + + if($tname == $tname2) { + $tname2 = $tname."2"; + $alias = $tname." AS ".$tname2; + } else + $alias = $tname2; + + $this->parts["join"][$tname][$tname2] = $join.$alias." ON ".$tname2.".id = ".$assocTableName.".".$fk->getForeign(); } $c = $table->getComponentName(); $this->joins[$name] = $c; + $table = $fk->getTable(); + $this->tableAliases[$name] = $tname2; } if( ! isset($this->tables[$name])) { diff --git a/Doctrine/Record.php b/Doctrine/Record.php index 9cc9af3ec..e0bed285f 100644 --- a/Doctrine/Record.php +++ b/Doctrine/Record.php @@ -1098,7 +1098,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite } elseif($fk instanceof Doctrine_Association) { $asf = $fk->getAssociationFactory(); $query = "SELECT ".$foreign." FROM ".$asf->getTableName()." WHERE ".$local." = ?"; - + $graph = new Doctrine_Query($table->getSession()); $query = "FROM ".$table->getComponentName()." WHERE ".$table->getComponentName().".".$table->getIdentifier()." IN ($query)"; diff --git a/Doctrine/Table.php b/Doctrine/Table.php index 971c7ba41..a6dbda2f3 100644 --- a/Doctrine/Table.php +++ b/Doctrine/Table.php @@ -87,7 +87,8 @@ class Doctrine_Table extends Doctrine_Configurable { */ private $boundAliases = array(); /** - * @var integer $columnCount cached column count + * @var integer $columnCount cached column count, Doctrine_Record uses this column count in when + * determining its state */ private $columnCount; @@ -323,6 +324,18 @@ class Doctrine_Table extends Doctrine_Configurable { final public function getSequenceName() { return $this->sequenceName; } + /** + * getParents + */ + final public function getParents() { + return $this->parents; + } + /** + * @return boolean + */ + final public function hasInheritanceMap() { + return (empty($this->inheritanceMap)); + } /** * setInheritanceMap * @param array $inheritanceMap diff --git a/tests/QueryTestCase.php b/tests/QueryTestCase.php index 95687a8b5..e9368dca3 100644 --- a/tests/QueryTestCase.php +++ b/tests/QueryTestCase.php @@ -14,7 +14,22 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase { $this->dbh->query("DROP TABLE IF EXISTS test_entries"); parent::prepareTables(); } - //public function prepareData() { } + + public function testManyToManyFetchingWithColumnAggregationInheritance() { + $query = new Doctrine_Query($this->session); + $query->from('User-l:Group-l'); + + $users = $query->execute(); + $this->assertEqual($users->count(), 1); + $this->assertEqual($users[0]->Group->count(), 1); + + $query->from('User-l.Group-l'); + $users = $query->execute(); + $this->assertEqual($users->count(), 8); + $this->assertEqual($users[0]->Group->count(), 0); + $this->assertEqual($users[1]->Group->count(), 1); + $this->assertEqual($users[2]->Group->count(), 0); + } public function testManyToManyFetchingWithColonOperator() { $query = new Doctrine_Query($this->session); @@ -77,7 +92,7 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase { $this->assertEqual($tasks[1]->ResourceAlias[1]->getState(), Doctrine_Record::STATE_PROXY); $this->assertEqual($tasks[1]->ResourceAlias[2]->getState(), Doctrine_Record::STATE_PROXY); $this->assertEqual($tasks[1]->ResourceAlias[3]->getState(), Doctrine_Record::STATE_PROXY); - + $count = count($this->dbh); $this->assertEqual($tasks[1]->ResourceAlias[0]->name, "R3"); @@ -90,6 +105,7 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase { $this->assertEqual($tasks[2]->ResourceAlias->count(), 1); $this->assertTrue($tasks[2]->ResourceAlias instanceof Doctrine_Collection_Lazy); } + public function testManyToManyFetchingWithDotOperator() { $query = new Doctrine_Query($this->session); @@ -847,7 +863,5 @@ class Doctrine_QueryTestCase extends Doctrine_UnitTestCase { //$this->assertTrue(isset($values['max'])); } - - } ?> diff --git a/tests/run.php b/tests/run.php index bc486415e..39ecc6136 100644 --- a/tests/run.php +++ b/tests/run.php @@ -44,9 +44,10 @@ $test->addTestCase(new Doctrine_Collection_OffsetTestCase()); $test->addTestCase(new Doctrine_CollectionTestCase()); +$test->addTestCase(new Doctrine_PessimisticLockingTestCase()); + $test->addTestCase(new Doctrine_QueryTestCase()); -$test->addTestCase(new Doctrine_PessimisticLockingTestCase()); //$test->addTestCase(new Doctrine_Cache_FileTestCase()); //$test->addTestCase(new Doctrine_Cache_SqliteTestCase());