From 1bbd52b8eed116c2beb3d5483f8ce8881b8450ee Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Mon, 20 Feb 2012 17:48:34 +0100 Subject: [PATCH] [DDC-1652] Fix SqlWalker to include foreign key identifiers in SQL SELECT statement no matter what the meta column setting is suggesting. --- .../Internal/Hydration/AbstractHydrator.php | 2 +- .../ORM/Internal/Hydration/ArrayHydrator.php | 2 +- lib/Doctrine/ORM/Query/SqlWalker.php | 15 ++++++++-- .../ORM/Functional/Ticket/DDC117Test.php | 28 +++++++++++++++++++ .../ORM/Query/SelectSqlGenerationTest.php | 2 +- 5 files changed, 44 insertions(+), 5 deletions(-) diff --git a/lib/Doctrine/ORM/Internal/Hydration/AbstractHydrator.php b/lib/Doctrine/ORM/Internal/Hydration/AbstractHydrator.php index e1d8cd1f2..020d48554 100644 --- a/lib/Doctrine/ORM/Internal/Hydration/AbstractHydrator.php +++ b/lib/Doctrine/ORM/Internal/Hydration/AbstractHydrator.php @@ -234,7 +234,7 @@ abstract class AbstractHydrator if (isset($cache[$key]['isScalar'])) { $value = $cache[$key]['type']->convertToPHPValue($value, $this->_platform); - + $rowData['scalars'][$cache[$key]['fieldName']] = $value; continue; diff --git a/lib/Doctrine/ORM/Internal/Hydration/ArrayHydrator.php b/lib/Doctrine/ORM/Internal/Hydration/ArrayHydrator.php index 20c2b5785..9a8fcee83 100644 --- a/lib/Doctrine/ORM/Internal/Hydration/ArrayHydrator.php +++ b/lib/Doctrine/ORM/Internal/Hydration/ArrayHydrator.php @@ -286,4 +286,4 @@ class ArrayHydrator extends AbstractHydrator return $this->_ce[$className]; } -} \ No newline at end of file +} diff --git a/lib/Doctrine/ORM/Query/SqlWalker.php b/lib/Doctrine/ORM/Query/SqlWalker.php index 13910054c..9701f4718 100644 --- a/lib/Doctrine/ORM/Query/SqlWalker.php +++ b/lib/Doctrine/ORM/Query/SqlWalker.php @@ -636,11 +636,17 @@ class SqlWalker implements TreeWalker } // Add foreign key columns to SQL, if necessary - if ( ! $addMetaColumns) continue; + if ( ! $addMetaColumns && ! $class->containsForeignIdentifier) { + continue; + } // Add foreign key columns of class and also parent classes foreach ($class->associationMappings as $assoc) { - if ( ! ($assoc['isOwningSide'] && $assoc['type'] & ClassMetadata::TO_ONE)) continue; + if ( ! ($assoc['isOwningSide'] && $assoc['type'] & ClassMetadata::TO_ONE)) { + continue; + } else if ( !$addMetaColumns && !isset($assoc['id'])) { + continue; + } $owningClass = (isset($assoc['inherited'])) ? $this->_em->getClassMetadata($assoc['inherited']) : $class; $sqlTableAlias = $this->getSQLTableAlias($owningClass->getTableName(), $dqlAlias); @@ -654,6 +660,11 @@ class SqlWalker implements TreeWalker } } + // Add foreign key columns to SQL, if necessary + if ( ! $addMetaColumns) { + continue; + } + // Add foreign key columns of subclasses foreach ($class->subClasses as $subClassName) { $subClass = $this->_em->getClassMetadata($subClassName); diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC117Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC117Test.php index bee6ef971..6ae4595dc 100644 --- a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC117Test.php +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC117Test.php @@ -433,4 +433,32 @@ class DDC117Test extends \Doctrine\Tests\OrmFunctionalTestCase $this->assertEquals($this->article1->id(), $refRep->source()->id()); $this->assertEquals($this->article2->id(), $refRep->target()->id()); } + + /** + * @group DDC-1652 + */ + public function testArrayHydrationWithCompositeKey() + { + $dql = "SELECT r,s,t FROM Doctrine\Tests\Models\DDC117\DDC117Reference r INNER JOIN r.source s INNER JOIN r.target t"; + $before = count($this->_em->createQuery($dql)->getResult()); + + $this->article1 = $this->_em->find("Doctrine\Tests\Models\DDC117\DDC117Article", $this->article1->id()); + $this->article2 = $this->_em->find("Doctrine\Tests\Models\DDC117\DDC117Article", $this->article2->id()); + + $this->reference = new DDC117Reference($this->article2, $this->article1, "Test-Description"); + $this->_em->persist($this->reference); + + $this->reference = new DDC117Reference($this->article1, $this->article1, "Test-Description"); + $this->_em->persist($this->reference); + + $this->reference = new DDC117Reference($this->article2, $this->article2, "Test-Description"); + $this->_em->persist($this->reference); + + $this->_em->flush(); + + $dql = "SELECT r,s,t FROM Doctrine\Tests\Models\DDC117\DDC117Reference r INNER JOIN r.source s INNER JOIN r.target t"; + $data = $this->_em->createQuery($dql)->getArrayResult(); + + $this->assertEquals($before + 3, count($data)); + } } diff --git a/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php b/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php index a7e96be3e..fccfed03b 100644 --- a/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php +++ b/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php @@ -1074,7 +1074,7 @@ class SelectSqlGenerationTest extends \Doctrine\Tests\OrmTestCase { $this->assertSqlGeneration( "SELECT t, s, l FROM Doctrine\Tests\Models\DDC117\DDC117Link l INNER JOIN l.target t INNER JOIN l.source s", - "SELECT d0_.article_id AS article_id0, d0_.title AS title1, d1_.article_id AS article_id2, d1_.title AS title3 FROM DDC117Link d2_ INNER JOIN DDC117Article d0_ ON d2_.target_id = d0_.article_id INNER JOIN DDC117Article d1_ ON d2_.source_id = d1_.article_id" + "SELECT d0_.article_id AS article_id0, d0_.title AS title1, d1_.article_id AS article_id2, d1_.title AS title3, d2_.source_id AS source_id4, d2_.target_id AS target_id5 FROM DDC117Link d2_ INNER JOIN DDC117Article d0_ ON d2_.target_id = d0_.article_id INNER JOIN DDC117Article d1_ ON d2_.source_id = d1_.article_id" ); }