From 778ed04eac753b7ead54d1d167d7677c2c2d2ff0 Mon Sep 17 00:00:00 2001 From: Karl Rixon Date: Tue, 17 Mar 2015 15:46:01 +0000 Subject: [PATCH 1/2] Support embeddables in partial object query expression [DDC-3621] --- lib/Doctrine/ORM/Query/Parser.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/Doctrine/ORM/Query/Parser.php b/lib/Doctrine/ORM/Query/Parser.php index 93f07b171..442dbac80 100644 --- a/lib/Doctrine/ORM/Query/Parser.php +++ b/lib/Doctrine/ORM/Query/Parser.php @@ -1784,8 +1784,16 @@ class Parser while ($this->lexer->isNextToken(Lexer::T_COMMA)) { $this->match(Lexer::T_COMMA); $this->match(Lexer::T_IDENTIFIER); - - $partialFieldSet[] = $this->lexer->token['value']; + + $field = $this->lexer->token['value']; + + while ($this->lexer->isNextToken(Lexer::T_DOT)) { + $this->match(Lexer::T_DOT); + $this->match(Lexer::T_IDENTIFIER); + $field .= '.'.$this->lexer->token['value']; + } + + $partialFieldSet[] = $field; } $this->match(Lexer::T_CLOSE_CURLY_BRACE); From 6070c9395dee672538cd53fd9dd87ae6bdaf891d Mon Sep 17 00:00:00 2001 From: Karl Rixon Date: Mon, 23 Mar 2015 15:44:06 +0000 Subject: [PATCH 2/2] Functional tests for partial selection with embeddables [DDC-3630] --- .../Tests/ORM/Functional/ValueObjectsTest.php | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/tests/Doctrine/Tests/ORM/Functional/ValueObjectsTest.php b/tests/Doctrine/Tests/ORM/Functional/ValueObjectsTest.php index de3ab7ee0..f7137b18b 100644 --- a/tests/Doctrine/Tests/ORM/Functional/ValueObjectsTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/ValueObjectsTest.php @@ -172,6 +172,42 @@ class ValueObjectsTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->_em->clear(); $this->assertNull($this->_em->find(__NAMESPACE__.'\\DDC93Person', $person->id)); } + + public function testPartialDqlOnEmbeddedObjectsField() + { + $person = new DDC93Person('Karl', new DDC93Address('Foo', '12345', 'Gosport', new DDC93Country('England'))); + $this->_em->persist($person); + $this->_em->flush($person); + $this->_em->clear(); + + // Prove that the entity was persisted correctly. + $dql = "SELECT p FROM " . __NAMESPACE__ ."\\DDC93Person p WHERE p.name = :name"; + + $person = $this->_em->createQuery($dql) + ->setParameter('name', 'Karl') + ->getSingleResult(); + + $this->assertEquals('Gosport', $person->address->city); + $this->assertEquals('Foo', $person->address->street); + $this->assertEquals('12345', $person->address->zip); + $this->assertEquals('England', $person->address->country->name); + + // Clear the EM and prove that the embeddable can be the subject of a partial query. + $this->_em->clear(); + + $dql = "SELECT PARTIAL p.{id,address.city} FROM " . __NAMESPACE__ ."\\DDC93Person p WHERE p.name = :name"; + + $person = $this->_em->createQuery($dql) + ->setParameter('name', 'Karl') + ->getSingleResult(); + + // Selected field must be equal, all other fields must be null. + $this->assertEquals('Gosport', $person->address->city); + $this->assertNull($person->address->street); + $this->assertNull($person->address->zip); + $this->assertNull($person->address->country); + $this->assertNull($person->name); + } public function testDqlWithNonExistentEmbeddableField() { @@ -180,6 +216,14 @@ class ValueObjectsTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->_em->createQuery("SELECT p FROM " . __NAMESPACE__ . "\\DDC93Person p WHERE p.address.asdfasdf IS NULL") ->execute(); } + + public function testPartialDqlWithNonExistentEmbeddableField() + { + $this->setExpectedException('Doctrine\ORM\Query\QueryException', "no mapped field named 'address.asdfasdf'"); + + $this->_em->createQuery("SELECT PARTIAL p.{id,address.asdfasdf} FROM " . __NAMESPACE__ . "\\DDC93Person p") + ->execute(); + } public function testEmbeddableWithInheritance() {