test discriminator column
This commit is contained in:
parent
0cc2583a02
commit
68665af6e8
@ -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;
|
||||
}
|
||||
}
|
@ -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"
|
||||
* ),
|
||||
* })
|
||||
*
|
||||
|
@ -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
|
||||
{
|
||||
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user