Merge pull request #1006 from Trainmaster/master
Handling invalid discriminator values
This commit is contained in:
commit
2da74e5147
@ -88,4 +88,18 @@ class HydrationException extends \Doctrine\ORM\ORMException
|
|||||||
$discrColumnName, $entityName, $dqlAlias
|
$discrColumnName, $entityName, $dqlAlias
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $discrValue
|
||||||
|
* @param array $discrMap
|
||||||
|
*
|
||||||
|
* @return HydrationException
|
||||||
|
*/
|
||||||
|
public static function invalidDiscriminatorValue($discrValue, $discrMap)
|
||||||
|
{
|
||||||
|
return new self(sprintf(
|
||||||
|
'The discriminator value "%s" is invalid. It must be one of "%s".',
|
||||||
|
$discrValue, implode('", "', $discrMap)
|
||||||
|
));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -266,7 +266,13 @@ class ObjectHydrator extends AbstractHydrator
|
|||||||
throw HydrationException::emptyDiscriminatorValue($dqlAlias);
|
throw HydrationException::emptyDiscriminatorValue($dqlAlias);
|
||||||
}
|
}
|
||||||
|
|
||||||
$className = $this->ce[$className]->discriminatorMap[$data[$discrColumn]];
|
$discrMap = $this->ce[$className]->discriminatorMap;
|
||||||
|
|
||||||
|
if ( ! isset($discrMap[$data[$discrColumn]])) {
|
||||||
|
throw HydrationException::invalidDiscriminatorValue($data[$discrColumn], array_keys($discrMap));
|
||||||
|
}
|
||||||
|
|
||||||
|
$className = $discrMap[$data[$discrColumn]];
|
||||||
|
|
||||||
unset($data[$discrColumn]);
|
unset($data[$discrColumn]);
|
||||||
}
|
}
|
||||||
|
@ -98,7 +98,13 @@ class SimpleObjectHydrator extends AbstractHydrator
|
|||||||
throw HydrationException::emptyDiscriminatorValue(key($this->_rsm->aliasMap));
|
throw HydrationException::emptyDiscriminatorValue(key($this->_rsm->aliasMap));
|
||||||
}
|
}
|
||||||
|
|
||||||
$entityName = $this->class->discriminatorMap[$sqlResult[$discrColumnName]];
|
$discrMap = $this->class->discriminatorMap;
|
||||||
|
|
||||||
|
if ( ! isset($discrMap[$sqlResult[$discrColumnName]])) {
|
||||||
|
throw HydrationException::invalidDiscriminatorValue($sqlResult[$discrColumnName], array_keys($discrMap));
|
||||||
|
}
|
||||||
|
|
||||||
|
$entityName = $discrMap[$sqlResult[$discrColumnName]];
|
||||||
|
|
||||||
unset($sqlResult[$discrColumnName]);
|
unset($sqlResult[$discrColumnName]);
|
||||||
}
|
}
|
||||||
|
@ -1926,4 +1926,34 @@ class ObjectHydratorTest extends HydrationTestCase
|
|||||||
$hydrator = new \Doctrine\ORM\Internal\Hydration\ObjectHydrator($this->_em);
|
$hydrator = new \Doctrine\ORM\Internal\Hydration\ObjectHydrator($this->_em);
|
||||||
$hydrator->hydrateAll($stmt, $rsm);
|
$hydrator->hydrateAll($stmt, $rsm);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @group DDC-3076
|
||||||
|
*
|
||||||
|
* @expectedException \Doctrine\ORM\Internal\Hydration\HydrationException
|
||||||
|
* @expectedExceptionMessage The discriminator value "subworker" is invalid. It must be one of "person", "manager", "employee".
|
||||||
|
*/
|
||||||
|
public function testInvalidDiscriminatorValueException()
|
||||||
|
{
|
||||||
|
$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(
|
||||||
|
'p__id' => '1',
|
||||||
|
'p__name' => 'Fabio B. Silva',
|
||||||
|
'discr' => 'subworker'
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
$stmt = new HydratorMockStatement($resultSet);
|
||||||
|
$hydrator = new \Doctrine\ORM\Internal\Hydration\ObjectHydrator($this->_em);
|
||||||
|
$hydrator->hydrateAll($stmt, $rsm);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -56,4 +56,34 @@ class SimpleObjectHydratorTest extends HydrationTestCase
|
|||||||
$result = $hydrator->hydrateAll($stmt, $rsm);
|
$result = $hydrator->hydrateAll($stmt, $rsm);
|
||||||
$this->assertEquals($result[0], $expectedEntity);
|
$this->assertEquals($result[0], $expectedEntity);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @group DDC-3076
|
||||||
|
*
|
||||||
|
* @expectedException \Doctrine\ORM\Internal\Hydration\HydrationException
|
||||||
|
* @expectedExceptionMessage The discriminator value "subworker" is invalid. It must be one of "person", "manager", "employee".
|
||||||
|
*/
|
||||||
|
public function testInvalidDiscriminatorValueException()
|
||||||
|
{
|
||||||
|
$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(
|
||||||
|
'p__id' => '1',
|
||||||
|
'p__name' => 'Fabio B. Silva',
|
||||||
|
'discr' => 'subworker'
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
$stmt = new HydratorMockStatement($resultSet);
|
||||||
|
$hydrator = new \Doctrine\ORM\Internal\Hydration\SimpleObjectHydrator($this->_em);
|
||||||
|
$hydrator->hydrateAll($stmt, $rsm);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user