From aeb2ab132b3f96a91def5ecbf82dbfb4e5c94bf7 Mon Sep 17 00:00:00 2001 From: "Fabio B. Silva" Date: Tue, 15 Nov 2011 20:27:45 -0200 Subject: [PATCH] group by all fields when entity has foreign keys --- lib/Doctrine/ORM/Query/SqlWalker.php | 11 ++++++++++- .../Tests/ORM/Functional/Ticket/DDC1430Test.php | 13 +------------ .../Tests/ORM/Query/SelectSqlGenerationTest.php | 16 ++++++++++++++++ 3 files changed, 27 insertions(+), 13 deletions(-) diff --git a/lib/Doctrine/ORM/Query/SqlWalker.php b/lib/Doctrine/ORM/Query/SqlWalker.php index d85fdde65..624f14b89 100644 --- a/lib/Doctrine/ORM/Query/SqlWalker.php +++ b/lib/Doctrine/ORM/Query/SqlWalker.php @@ -22,7 +22,8 @@ namespace Doctrine\ORM\Query; use Doctrine\DBAL\LockMode, Doctrine\ORM\Mapping\ClassMetadata, Doctrine\ORM\Query, - Doctrine\ORM\Query\QueryException; + Doctrine\ORM\Query\QueryException, + Doctrine\ORM\Mapping\ClassMetadataInfo; /** * The SqlWalker is a TreeWalker that walks over a DQL AST and constructs @@ -1307,6 +1308,14 @@ class SqlWalker implements TreeWalker $item->type = AST\PathExpression::TYPE_STATE_FIELD; $sqlParts[] = $this->walkGroupByItem($item); } + + foreach ($this->_queryComponents[$groupByItem]['metadata']->associationMappings AS $mapping) { + if ($mapping['isOwningSide'] && $mapping['type'] & ClassMetadataInfo::TO_ONE) { + $item = new AST\PathExpression(AST\PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION, $groupByItem, $mapping['fieldName']); + $item->type = AST\PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION; + $sqlParts[] = $this->walkGroupByItem($item); + } + } } return ' GROUP BY ' . implode(', ', $sqlParts); diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1430Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1430Test.php index 423a73649..4e08a904c 100644 --- a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1430Test.php +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1430Test.php @@ -110,20 +110,9 @@ class DDC1430Test extends \Doctrine\Tests\OrmFunctionalTestCase $this->assertEquals($result[0]['p_count'], 2); $this->assertEquals($result[1]['p_count'], 3); } - - - - public function testWithForeignKeys() - { - $query = $this->_em->createQuery('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u GROUP BY u'); - $this->assertEquals('SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3, c0_.email_id AS email_id4 FROM cms_users c0_ GROUP BY c0_.id, c0_.status, c0_.username, c0_.name', $query->getSQL()); - - $query = $this->_em->createQuery('SELECT e FROM Doctrine\Tests\Models\CMS\CmsEmployee e GROUP BY e'); - $this->assertEquals('SELECT c0_.id AS id0, c0_.name AS name1, c0_.spouse_id AS spouse_id2 FROM cms_employees c0_ GROUP BY c0_.id, c0_.name', $query->getSQL()); - } public function loadFixtures() - { + { $o1 = new DDC1430Order('NEW'); $o2 = new DDC1430Order('OK'); diff --git a/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php b/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php index 62ded22bf..7ddfe77b8 100644 --- a/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php +++ b/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php @@ -1317,6 +1317,22 @@ class SelectSqlGenerationTest extends \Doctrine\Tests\OrmTestCase 'SELECT d0_.id AS id0, +d0_.value AS sclr1 FROM DDC1474Entity d0_' ); } + + /** + * @group DDC-1430 + */ + public function testGroupByAllFieldsWhenObjectHasForeignKeys() + { + $this->assertSqlGeneration( + 'SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u GROUP BY u', + 'SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 FROM cms_users c0_ GROUP BY c0_.id, c0_.status, c0_.username, c0_.name, c0_.email_id' + ); + + $this->assertSqlGeneration( + 'SELECT e FROM Doctrine\Tests\Models\CMS\CmsEmployee e GROUP BY e', + 'SELECT c0_.id AS id0, c0_.name AS name1 FROM cms_employees c0_ GROUP BY c0_.id, c0_.name, c0_.spouse_id' + ); + } }