From f018a56d6dbf587865fc59ee2ee5970724f74e29 Mon Sep 17 00:00:00 2001 From: "Fabio B. Silva" Date: Mon, 27 Feb 2012 21:16:35 -0300 Subject: [PATCH] test native query with result class --- .../ORM/Query/ResultSetMappingBuilder.php | 9 +++ tests/Doctrine/Tests/Models/CMS/CmsUser.php | 13 +++++ .../Tests/ORM/Functional/NativeQueryTest.php | 57 ++++++++++++++++++- .../Doctrine/Tests/OrmFunctionalTestCase.php | 1 + 4 files changed, 79 insertions(+), 1 deletion(-) diff --git a/lib/Doctrine/ORM/Query/ResultSetMappingBuilder.php b/lib/Doctrine/ORM/Query/ResultSetMappingBuilder.php index 37a3ea45a..fcb12ecc2 100644 --- a/lib/Doctrine/ORM/Query/ResultSetMappingBuilder.php +++ b/lib/Doctrine/ORM/Query/ResultSetMappingBuilder.php @@ -129,6 +129,15 @@ class ResultSetMappingBuilder extends ResultSetMapping $this->addFieldResult($alias, $columnName, $propertyName); } + foreach ($classMetadata->associationMappings as $associationMapping) { + if ($associationMapping['isOwningSide'] && $associationMapping['type'] & ClassMetadataInfo::TO_ONE) { + foreach ($associationMapping['joinColumns'] as $joinColumn) { + $columnName = $joinColumn['name']; + $this->addMetaResult($alias, $columnName, $columnName, $classMetadata->isIdentifier($columnName)); + } + } + } + return $this; } diff --git a/tests/Doctrine/Tests/Models/CMS/CmsUser.php b/tests/Doctrine/Tests/Models/CMS/CmsUser.php index 1e5465784..a388081e6 100644 --- a/tests/Doctrine/Tests/Models/CMS/CmsUser.php +++ b/tests/Doctrine/Tests/Models/CMS/CmsUser.php @@ -10,6 +10,19 @@ use Doctrine\Common\Collections\ArrayCollection; * @NamedQueries({ * @NamedQuery(name="all", query="SELECT u FROM __CLASS__ u") * }) + * + * @NamedNativeQueries({ + * @NamedNativeQuery( + * name = "fetchIdAndUsernameWithResultClass", + * resultClass = "CmsUser", + * query = "SELECT id, username FROM cms_users WHERE username = ?" + * ), + * @NamedNativeQuery( + * name = "fetchAllColumnsWithResultClass", + * resultClass = "CmsUser", + * query = "SELECT * FROM cms_users WHERE username = ?" + * ), + * }) */ class CmsUser { diff --git a/tests/Doctrine/Tests/ORM/Functional/NativeQueryTest.php b/tests/Doctrine/Tests/ORM/Functional/NativeQueryTest.php index db02b3d4b..80c580366 100644 --- a/tests/Doctrine/Tests/ORM/Functional/NativeQueryTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/NativeQueryTest.php @@ -7,6 +7,7 @@ use Doctrine\ORM\Query\ResultSetMappingBuilder; use Doctrine\Tests\Models\CMS\CmsUser; use Doctrine\Tests\Models\CMS\CmsPhonenumber; use Doctrine\Tests\Models\CMS\CmsAddress; +use Doctrine\Tests\Models\CMS\CmsEmail; use Doctrine\Tests\Models\Company\CompanyFixContract; use Doctrine\Tests\Models\Company\CompanyEmployee; @@ -334,7 +335,7 @@ class NativeQueryTest extends \Doctrine\Tests\OrmFunctionalTestCase /** * @group DDC-1663 */ - public function testBasicNativeNamedQuery() + public function testBasicNativeNamedQueryWithSqlResultSetMapping() { $user = new CmsUser; $user->name = 'Fabio B. Silva'; @@ -365,5 +366,59 @@ class NativeQueryTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->assertEquals($addr->city, $result[0]->city); $this->assertEquals($addr->country, $result[0]->country); } + + /** + * @group DDC-1663 + */ + public function testBasicNativeQueryWithResultClass() + { + //$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger); + + $user = new CmsUser; + $user->name = 'Fabio B. Silva'; + $user->username = 'FabioBatSilva'; + $user->status = 'dev'; + + $email = new CmsEmail(); + $email->email = 'fabio.bat.silva@gmail.com'; + + $user->setEmail($email); + + $this->_em->clear(); + $this->_em->persist($user); + $this->_em->flush(); + + $this->_em->clear(); + + $repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser'); + + + $result = $repository->createNativeNamedQuery('fetchIdAndUsernameWithResultClass') + ->setParameter(1, 'FabioBatSilva')->getResult(); + $this->assertEquals(1, count($result)); + $this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsUser', $result[0]); + $this->assertNull($result[0]->name); + $this->assertNull($result[0]->email); + $this->assertEquals($user->id, $result[0]->id); + $this->assertEquals('FabioBatSilva', $result[0]->username); + + $this->_em->clear(); + + + $result = $repository->createNativeNamedQuery('fetchAllColumnsWithResultClass') + ->setParameter(1, 'FabioBatSilva')->getResult(); + + $this->assertEquals(1, count($result)); + $this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsUser', $result[0]); + $this->assertEquals($user->id, $result[0]->id); + $this->assertEquals('Fabio B. Silva', $result[0]->name); + $this->assertEquals('FabioBatSilva', $result[0]->username); + $this->assertEquals('dev', $result[0]->status); + $this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsEmail', $result[0]->email); + + + $this->_em->clear(); + + } } diff --git a/tests/Doctrine/Tests/OrmFunctionalTestCase.php b/tests/Doctrine/Tests/OrmFunctionalTestCase.php index 4b92ed5d3..9d9776ba9 100644 --- a/tests/Doctrine/Tests/OrmFunctionalTestCase.php +++ b/tests/Doctrine/Tests/OrmFunctionalTestCase.php @@ -147,6 +147,7 @@ abstract class OrmFunctionalTestCase extends OrmTestCase $conn->executeUpdate('DELETE FROM cms_comments'); $conn->executeUpdate('DELETE FROM cms_articles'); $conn->executeUpdate('DELETE FROM cms_users'); + $conn->executeUpdate('DELETE FROM cms_emails'); } if (isset($this->_usedModelSets['ecommerce'])) {