From c1091485b009c01193b087e4eb7ffc80824fbfdd Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Tue, 29 Jun 2010 00:06:19 +0200 Subject: [PATCH] DDC-618 - Bugfix INDEX BY was not yet implemented in SqlWalker --- lib/Doctrine/ORM/Query/Parser.php | 2 +- lib/Doctrine/ORM/Query/SqlWalker.php | 7 ++ .../ORM/Functional/Ticket/DDC618Test.php | 82 +++++++++++++++++++ 3 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 tests/Doctrine/Tests/ORM/Functional/Ticket/DDC618Test.php diff --git a/lib/Doctrine/ORM/Query/Parser.php b/lib/Doctrine/ORM/Query/Parser.php index 70ec16484..009941cfb 100644 --- a/lib/Doctrine/ORM/Query/Parser.php +++ b/lib/Doctrine/ORM/Query/Parser.php @@ -1610,7 +1610,7 @@ class Parser $parts = $pathExp->parts; $this->_queryComponents[$pathExp->identificationVariable]['map'] = $parts[0]; - return $pathExp; + return new AST\IndexBy($pathExp); } /** diff --git a/lib/Doctrine/ORM/Query/SqlWalker.php b/lib/Doctrine/ORM/Query/SqlWalker.php index 36e37bab0..289e0200d 100644 --- a/lib/Doctrine/ORM/Query/SqlWalker.php +++ b/lib/Doctrine/ORM/Query/SqlWalker.php @@ -617,6 +617,13 @@ class SqlWalker implements TreeWalker $sql .= $this->walkJoinVariableDeclaration($joinVarDecl); } + if ($firstIdentificationVarDecl->indexBy) { + $this->_rsm->addIndexBy( + $firstIdentificationVarDecl->indexBy->simpleStateFieldPathExpression->identificationVariable, + $firstIdentificationVarDecl->indexBy->simpleStateFieldPathExpression->parts[0] + ); + } + return $this->_platform->appendLockHint($sql, $this->_query->getHint(Query::HINT_LOCK_MODE)); } diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC618Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC618Test.php new file mode 100644 index 000000000..2d007553f --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC618Test.php @@ -0,0 +1,82 @@ +_schemaTool->createSchema(array( + $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC618Author') + )); + + // Create author 10/Joe with two books 22/JoeA and 20/JoeB + $author = new DDC618Author(); + $author->id = 10; + $author->name = 'Joe'; + $this->_em->persist($author); + + // Create author 11/Alice with two books 21/AliceA and 23/AliceB + $author = new DDC618Author(); + $author->id = 11; + $author->name = 'Alice'; + $this->_em->persist($author); + + $this->_em->flush(); + $this->_em->clear(); + } catch(\Exception $e) { + + } + } + + public function testIndexByHydrateObject() + { + $dql = 'SELECT A FROM Doctrine\Tests\ORM\Functional\Ticket\DDC618Author A INDEX BY A.name ORDER BY A.name ASC'; + $result = $this->_em->createQuery($dql)->getResult(\Doctrine\ORM\Query::HYDRATE_OBJECT); + + $joe = $this->_em->find('Doctrine\Tests\ORM\Functional\Ticket\DDC618Author', 10); + $alice = $this->_em->find('Doctrine\Tests\ORM\Functional\Ticket\DDC618Author', 11); + + $this->assertArrayHasKey('Joe', $result, "INDEX BY A.name should return an index by the name of 'Joe'."); + $this->assertArrayHasKey('Alice', $result, "INDEX BY A.name should return an index by the name of 'Alice'."); + } + + public function testIndexByHydrateArray() + { + $dql = 'SELECT A FROM Doctrine\Tests\ORM\Functional\Ticket\DDC618Author A INDEX BY A.name ORDER BY A.name ASC'; + $result = $this->_em->createQuery($dql)->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY); + + $joe = $this->_em->find('Doctrine\Tests\ORM\Functional\Ticket\DDC618Author', 10); + $alice = $this->_em->find('Doctrine\Tests\ORM\Functional\Ticket\DDC618Author', 11); + + $this->assertArrayHasKey('Joe', $result, "INDEX BY A.name should return an index by the name of 'Joe'."); + $this->assertArrayHasKey('Alice', $result, "INDEX BY A.name should return an index by the name of 'Alice'."); + } +} + +/** + * @Entity + * @Table (name="ddc618author", uniqueConstraints={ @Index (name="UQ_authorname", columns={ "name" }) }) + */ +class DDC618Author +{ + /** + * @Id + * @Column(type="integer") + */ + public $id; + + /** @Column(type="string") */ + public $name; + + public function __construct() + { + $this->books = new \Doctrine\Common\Collections\ArrayCollection; + } +} \ No newline at end of file