DDC-1184 - Improve error handling in AssignedIdGenerator
This commit is contained in:
parent
f3677554e8
commit
70d756d59c
@ -47,9 +47,13 @@ class AssignedGenerator extends AbstractIdGenerator
|
|||||||
if ($class->isIdentifierComposite) {
|
if ($class->isIdentifierComposite) {
|
||||||
$idFields = $class->getIdentifierFieldNames();
|
$idFields = $class->getIdentifierFieldNames();
|
||||||
foreach ($idFields as $idField) {
|
foreach ($idFields as $idField) {
|
||||||
$value = $class->getReflectionProperty($idField)->getValue($entity);
|
$value = $class->reflFields[$idField]->getValue($entity);
|
||||||
if (isset($value)) {
|
if (isset($value)) {
|
||||||
if (is_object($value)) {
|
if (is_object($value)) {
|
||||||
|
if (!$em->getUnitOfWork()->isInIdentityMap($value)) {
|
||||||
|
throw ORMException::entityMissingForeignAssignedId($entity, $value);
|
||||||
|
}
|
||||||
|
|
||||||
// NOTE: Single Columns as associated identifiers only allowed - this constraint it is enforced.
|
// NOTE: Single Columns as associated identifiers only allowed - this constraint it is enforced.
|
||||||
$identifier[$idField] = current($em->getUnitOfWork()->getEntityIdentifier($value));
|
$identifier[$idField] = current($em->getUnitOfWork()->getEntityIdentifier($value));
|
||||||
} else {
|
} else {
|
||||||
@ -64,6 +68,10 @@ class AssignedGenerator extends AbstractIdGenerator
|
|||||||
$value = $class->reflFields[$idField]->getValue($entity);
|
$value = $class->reflFields[$idField]->getValue($entity);
|
||||||
if (isset($value)) {
|
if (isset($value)) {
|
||||||
if (is_object($value)) {
|
if (is_object($value)) {
|
||||||
|
if (!$em->getUnitOfWork()->isInIdentityMap($value)) {
|
||||||
|
throw ORMException::entityMissingForeignAssignedId($entity, $value);
|
||||||
|
}
|
||||||
|
|
||||||
// NOTE: Single Columns as associated identifiers only allowed - this constraint it is enforced.
|
// NOTE: Single Columns as associated identifiers only allowed - this constraint it is enforced.
|
||||||
$identifier[$idField] = current($em->getUnitOfWork()->getEntityIdentifier($value));
|
$identifier[$idField] = current($em->getUnitOfWork()->getEntityIdentifier($value));
|
||||||
} else {
|
} else {
|
||||||
|
@ -35,9 +35,24 @@ class ORMException extends Exception
|
|||||||
"to Doctrine\ORM\Configuration::setMetadataDriverImpl().");
|
"to Doctrine\ORM\Configuration::setMetadataDriverImpl().");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function entityMissingForeignAssignedId($entity, $relatedEntity)
|
||||||
|
{
|
||||||
|
return new self(
|
||||||
|
"Entity of type " . get_class($entity) . " has identity through a foreign entity " . get_class($relatedEntityClass) . ", " .
|
||||||
|
"however this entity has no ientity itself. You have to call EntityManager#persist() on the related entity " .
|
||||||
|
"and make sure it an identifier was generated before trying to persist '" . get_class($entity) . "'. In case " .
|
||||||
|
"of Post Insert ID Generation (such as MySQL Auto-Increment or PostgreSQL SERIAL) this means you have to call " .
|
||||||
|
"EntityManager#flush() between both persist operations."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
public static function entityMissingAssignedId($entity)
|
public static function entityMissingAssignedId($entity)
|
||||||
{
|
{
|
||||||
return new self("Entity of type " . get_class($entity) . " is missing an assigned ID.");
|
return new self("Entity of type " . get_class($entity) . " is missing an assigned ID. " .
|
||||||
|
"The identifier generation strategy for this entity requires the ID field to be populated before ".
|
||||||
|
"EntityManager#persist() is called. If you want automatically generated identifiers instead " .
|
||||||
|
"you need to adjust the metadata mapping accordingly."
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function unrecognizedField($field)
|
public static function unrecognizedField($field)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user