From 68665af6e8da91bc1ea350ba02c7e6ba65aae963 Mon Sep 17 00:00:00 2001 From: "Fabio B. Silva" Date: Tue, 28 Feb 2012 23:17:29 -0300 Subject: [PATCH] test discriminator column --- .../ORM/Query/ResultSetMappingBuilder.php | 14 +++++- tests/Doctrine/Tests/Models/CMS/CmsUser.php | 2 +- .../Tests/Models/Company/CompanyPerson.php | 36 +++++++++++-- .../Tests/ORM/Functional/NativeQueryTest.php | 50 +++++++++++++++++++ 4 files changed, 97 insertions(+), 5 deletions(-) diff --git a/lib/Doctrine/ORM/Query/ResultSetMappingBuilder.php b/lib/Doctrine/ORM/Query/ResultSetMappingBuilder.php index 310e0c2d9..e334665db 100644 --- a/lib/Doctrine/ORM/Query/ResultSetMappingBuilder.php +++ b/lib/Doctrine/ORM/Query/ResultSetMappingBuilder.php @@ -124,6 +124,12 @@ class ResultSetMappingBuilder extends ResultSetMapping $this->addEntityResult($class->name, $alias); + if ($classMetadata->discriminatorColumn) { + $discriminatorColumn = $classMetadata->discriminatorColumn; + $this->setDiscriminatorColumn($alias, $discriminatorColumn['name']); + $this->addMetaResult($alias, $discriminatorColumn['name'], $discriminatorColumn['fieldName']); + } + foreach ($classMetadata->getColumnNames() as $key => $columnName) { $propertyName = $classMetadata->getFieldName($columnName); $this->addFieldResult($alias, $columnName, $propertyName); @@ -147,6 +153,12 @@ class ResultSetMappingBuilder extends ResultSetMapping $classMetadata = $this->em->getClassMetadata($entityMapping['entityClass']); $shortName = $classMetadata->reflClass->getShortName(); $alias = strtolower($shortName[0]) . $key; + + if (isset($entityMapping['discriminatorColumn']) && $entityMapping['discriminatorColumn']) { + $discriminatorColumn = $entityMapping['discriminatorColumn']; + $this->setDiscriminatorColumn($alias, $discriminatorColumn); + $this->addMetaResult($alias, $discriminatorColumn, $discriminatorColumn); + } $this->addEntityResult($classMetadata->name, $alias); if (isset($entityMapping['fields']) && !empty($entityMapping['fields'])) { @@ -191,7 +203,7 @@ class ResultSetMappingBuilder extends ResultSetMapping $this->addScalarResult($entityMapping['name'], $entityMapping['name']); } } - + return $this; } } \ No newline at end of file diff --git a/tests/Doctrine/Tests/Models/CMS/CmsUser.php b/tests/Doctrine/Tests/Models/CMS/CmsUser.php index 348f29329..bec1b143d 100644 --- a/tests/Doctrine/Tests/Models/CMS/CmsUser.php +++ b/tests/Doctrine/Tests/Models/CMS/CmsUser.php @@ -35,7 +35,7 @@ use Doctrine\Common\Collections\ArrayCollection; * @NamedNativeQuery( * name = "fetchUserPhonenumberCount", * resultSetMapping= "mappingUserPhonenumberCount", - * query = "SELECT id, name, status, COUNT(phonenumber) AS numPhones FROM cms_users INNER JOIN cms_phonenumbers ON id = user_id WHERE username IN (?) GROUP BY status, user_id ORDER BY username" + * query = "SELECT id, name, status, COUNT(phonenumber) AS numPhones FROM cms_users INNER JOIN cms_phonenumbers ON id = user_id WHERE username IN (?) GROUP BY id, name, status ORDER BY username" * ), * }) * diff --git a/tests/Doctrine/Tests/Models/Company/CompanyPerson.php b/tests/Doctrine/Tests/Models/Company/CompanyPerson.php index 0dfe9191c..f1cc318db 100644 --- a/tests/Doctrine/Tests/Models/Company/CompanyPerson.php +++ b/tests/Doctrine/Tests/Models/Company/CompanyPerson.php @@ -11,9 +11,39 @@ namespace Doctrine\Tests\Models\Company; * @InheritanceType("JOINED") * @DiscriminatorColumn(name="discr", type="string") * @DiscriminatorMap({ - * "person" = "CompanyPerson", - * "manager" = "CompanyManager", - * "employee" = "CompanyEmployee"}) + * "person" = "CompanyPerson", + * "manager" = "CompanyManager", + * "employee" = "CompanyEmployee" + * }) + * + * @NamedNativeQueries({ + * @NamedNativeQuery( + * name = "fetchAllWithResultClass", + * resultClass = "__CLASS__", + * query = "SELECT id, name, discr FROM company_persons ORDER BY name" + * ), + * @NamedNativeQuery( + * name = "fetchAllWithSqlResultSetMapping", + * resultSetMapping= "mappingFetchAll", + * query = "SELECT id, name, discr AS discriminator FROM company_persons ORDER BY name" + * ) + * }) + * + * @SqlResultSetMappings({ + * @SqlResultSetMapping( + * name = "mappingFetchAll", + * entities= { + * @EntityResult( + * entityClass = "__CLASS__", + * discriminatorColumn = "discriminator", + * fields = { + * @FieldResult("id"), + * @FieldResult("name"), + * } + * ) + * } + * ) + * }) */ class CompanyPerson { diff --git a/tests/Doctrine/Tests/ORM/Functional/NativeQueryTest.php b/tests/Doctrine/Tests/ORM/Functional/NativeQueryTest.php index 8c9657fda..12dbbc6d4 100644 --- a/tests/Doctrine/Tests/ORM/Functional/NativeQueryTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/NativeQueryTest.php @@ -10,6 +10,7 @@ use Doctrine\Tests\Models\CMS\CmsAddress; use Doctrine\Tests\Models\CMS\CmsEmail; use Doctrine\Tests\Models\Company\CompanyFixContract; use Doctrine\Tests\Models\Company\CompanyEmployee; +use Doctrine\Tests\Models\Company\CompanyPerson; require_once __DIR__ . '/../../TestInit.php'; @@ -24,6 +25,7 @@ class NativeQueryTest extends \Doctrine\Tests\OrmFunctionalTestCase protected function setUp() { $this->useModelSet('cms'); + $this->useModelSet('company'); parent::setUp(); $this->platform = $this->_em->getConnection()->getDatabasePlatform(); } @@ -546,4 +548,52 @@ class NativeQueryTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->assertEquals(1, $result[1]['numPhones']); } + /** + * @group DDC-1663 + */ + public function testNativeNamedQueryInheritance() + { + $person = new CompanyPerson; + $person->setName('Fabio B. Silva'); + + $employee = new CompanyEmployee; + $employee->setName('Fabio Silva'); + $employee->setSalary(100000); + $employee->setDepartment('IT'); + + $this->_em->persist($person); + $this->_em->persist($employee); + + $this->_em->flush(); + $this->_em->clear(); + + $repository = $this->_em->getRepository('Doctrine\Tests\Models\Company\CompanyPerson'); + + $result = $repository->createNativeNamedQuery('fetchAllWithSqlResultSetMapping') + ->getResult(); + + $this->assertEquals(2, count($result)); + $this->assertInstanceOf('Doctrine\Tests\Models\Company\CompanyPerson', $result[0]); + $this->assertInstanceOf('Doctrine\Tests\Models\Company\CompanyEmployee', $result[1]); + $this->assertTrue(is_numeric($result[0]->getId())); + $this->assertTrue(is_numeric($result[1]->getId())); + $this->assertEquals('Fabio B. Silva', $result[0]->getName()); + $this->assertEquals('Fabio Silva', $result[1]->getName()); + + + $this->_em->clear(); + + + $result = $repository->createNativeNamedQuery('fetchAllWithResultClass') + ->getResult(); + + $this->assertEquals(2, count($result)); + $this->assertInstanceOf('Doctrine\Tests\Models\Company\CompanyPerson', $result[0]); + $this->assertInstanceOf('Doctrine\Tests\Models\Company\CompanyEmployee', $result[1]); + $this->assertTrue(is_numeric($result[0]->getId())); + $this->assertTrue(is_numeric($result[1]->getId())); + $this->assertEquals('Fabio B. Silva', $result[0]->getName()); + $this->assertEquals('Fabio Silva', $result[1]->getName()); + } + } \ No newline at end of file