1
0
mirror of synced 2024-12-14 15:16:04 +03:00

Fix DDC-1470

This commit is contained in:
Fabio B. Silva 2012-03-15 21:29:07 -03:00
parent 44d7d23e8d
commit 36dc560533
5 changed files with 141 additions and 0 deletions

View File

@ -23,4 +23,30 @@ class HydrationException extends \Doctrine\ORM\ORMException
"discriminator value in a table row."
);
}
/**
* @since 2.3
* @param string $entityName
* @param string $discrColumnName
* @param string $dqlAlias
* @return HydrationException
*/
public static function missingDiscriminatorColumn($entityName, $discrColumnName, $dqlAlias)
{
return new self("The discriminator column '$discrColumnName' "
. "is missing for '$entityName' using the DQL alias '$dqlAlias'.");
}
/**
* @since 2.3
* @param string $entityName
* @param string $discrColumnName
* @param string $dqlAlias
* @return HydrationException
*/
public static function missingDiscriminatorMetaMappingColumn($entityName, $discrColumnName, $dqlAlias)
{
return new self("The meta mapping for the discriminator column '$discrColumnName' "
. "is missing for '$entityName' using the DQL alias '$dqlAlias'.");
}
}

View File

@ -217,8 +217,17 @@ class ObjectHydrator extends AbstractHydrator
$className = $this->_rsm->aliasMap[$dqlAlias];
if (isset($this->_rsm->discriminatorColumns[$dqlAlias])) {
if (!isset($this->_rsm->metaMappings[$this->_rsm->discriminatorColumns[$dqlAlias]])) {
throw HydrationException::missingDiscriminatorMetaMappingColumn($className, $this->_rsm->discriminatorColumns[$dqlAlias], $dqlAlias);
}
$discrColumn = $this->_rsm->metaMappings[$this->_rsm->discriminatorColumns[$dqlAlias]];
if (!isset($data[$discrColumn])) {
throw HydrationException::missingDiscriminatorColumn($className, $discrColumn, $dqlAlias);
}
if ($data[$discrColumn] === "") {
throw HydrationException::emptyDiscriminatorValue($dqlAlias);
}

View File

@ -92,6 +92,10 @@ class SimpleObjectHydrator extends AbstractHydrator
if ($this->class->inheritanceType !== ClassMetadata::INHERITANCE_TYPE_NONE) {
$discrColumnName = $this->_platform->getSQLResultCasing($this->class->discriminatorColumn['name']);
if (!isset($sqlResult[$discrColumnName])) {
throw HydrationException::missingDiscriminatorColumn($entityName, $discrColumnName, key($this->_rsm->aliasMap));
}
if ($sqlResult[$discrColumnName] === '') {
throw HydrationException::emptyDiscriminatorValue(key($this->_rsm->aliasMap));
}

View File

@ -1863,4 +1863,69 @@ class ObjectHydratorTest extends HydrationTestCase
$result
);
}
/**
* @group DDC-1470
*
* @expectedException \Doctrine\ORM\Internal\Hydration\HydrationException
* @expectedExceptionMessage The meta mapping for the discriminator column 'c_discr' is missing for 'Doctrine\Tests\Models\Company\CompanyFixContract' using the DQL alias 'c'.
*/
public function testMissingMetaMappingException()
{
$rsm = new ResultSetMapping;
$rsm->addEntityResult('Doctrine\Tests\Models\Company\CompanyFixContract', 'c');
$rsm->addJoinedEntityResult('Doctrine\Tests\Models\Company\CompanyEmployee', 'e', 'c', 'salesPerson');
$rsm->addFieldResult('c', 'c__id', 'id');
$rsm->setDiscriminatorColumn('c', 'c_discr');
$resultSet = array(
array(
'c__id' => '1',
'c_discr' => 'fix',
),
);
$stmt = new HydratorMockStatement($resultSet);
$hydrator = new \Doctrine\ORM\Internal\Hydration\ObjectHydrator($this->_em);
$hydrator->hydrateAll($stmt, $rsm);
}
/**
* @group DDC-1470
*
* @expectedException \Doctrine\ORM\Internal\Hydration\HydrationException
* @expectedExceptionMessage The discriminator column 'discr' is missing for 'Doctrine\Tests\Models\Company\CompanyEmployee' using the DQL alias 'e'.
*/
public function testMissingDiscriminatorColumnException()
{
$rsm = new ResultSetMapping;
$rsm->addEntityResult('Doctrine\Tests\Models\Company\CompanyFixContract', 'c');
$rsm->addJoinedEntityResult('Doctrine\Tests\Models\Company\CompanyEmployee', 'e', 'c', 'salesPerson');
$rsm->addFieldResult('c', 'c__id', 'id');
$rsm->addMetaResult('c', 'c_discr', 'discr');
$rsm->setDiscriminatorColumn('c', 'c_discr');
$rsm->addFieldResult('e', 'e__id', 'id');
$rsm->addFieldResult('e', 'e__name', 'name');
$rsm->addMetaResult('e ', 'e_discr', 'discr');
$rsm->setDiscriminatorColumn('e', 'e_discr');
$resultSet = array(
array(
'c__id' => '1',
'c_discr' => 'fix',
'e__id' => '1',
'e__name' => 'Fabio B. Silva'
),
);
$stmt = new HydratorMockStatement($resultSet);
$hydrator = new \Doctrine\ORM\Internal\Hydration\ObjectHydrator($this->_em);
$hydrator->hydrateAll($stmt, $rsm);
}
}

View File

@ -0,0 +1,37 @@
<?php
namespace Doctrine\Tests\ORM\Hydration;
use Doctrine\Tests\Mocks\HydratorMockStatement;
use Doctrine\ORM\Query\ResultSetMapping;
require_once __DIR__ . '/../../TestInit.php';
class SimpleObjectHydratorTest extends HydrationTestCase
{
/**
* @group DDC-1470
*
* @expectedException \Doctrine\ORM\Internal\Hydration\HydrationException
* @expectedExceptionMessage The discriminator column 'discr' is missing for 'Doctrine\Tests\Models\Company\CompanyPerson' using the DQL alias 'p'.
*/
public function testMissingDiscriminatorColumnException()
{
$rsm = new ResultSetMapping;
$rsm->addEntityResult('Doctrine\Tests\Models\Company\CompanyPerson', 'p');
$rsm->addFieldResult('p', 'p__id', 'id');
$rsm->addFieldResult('p', 'p__name', 'name');
$rsm->addMetaResult('p ', 'discr', 'discr');
$rsm->setDiscriminatorColumn('p', 'discr');
$resultSet = array(
array(
'u__id' => '1',
'u__name' => 'Fabio B. Silva'
),
);
$stmt = new HydratorMockStatement($resultSet);
$hydrator = new \Doctrine\ORM\Internal\Hydration\SimpleObjectHydrator($this->_em);
$hydrator->hydrateAll($stmt, $rsm);
}
}