From d0c0f43c79a9f3ac42cd74ac845b6392c03d9d71 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Sun, 18 Jan 2015 00:53:57 +0100 Subject: [PATCH] #1228 DDC-3490 - preventing invalid association values from being persisted --- lib/Doctrine/ORM/UnitOfWork.php | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/Doctrine/ORM/UnitOfWork.php b/lib/Doctrine/ORM/UnitOfWork.php index c022a1ae4..9764a6eca 100644 --- a/lib/Doctrine/ORM/UnitOfWork.php +++ b/lib/Doctrine/ORM/UnitOfWork.php @@ -731,7 +731,7 @@ class UnitOfWork implements PropertyChangedListener try { $this->computeAssociationChanges($assoc, $val); } catch (\Exception $ex) { - throw ORMInvalidArgumentException::computeAssociationChangesError($entity, $assoc['fieldName'], $ex->value); + throw ORMInvalidArgumentException::computeAssociationChangesError($entity, $assoc['fieldName'], $val); } if ( ! isset($this->entityChangeSets[$oid]) && @@ -2188,12 +2188,31 @@ class UnitOfWork implements PropertyChangedListener case ($relatedEntities instanceof Collection): case (is_array($relatedEntities)): + if (($assoc['type'] & ClassMetadata::TO_MANY) <= 0) { + throw ORMInvalidArgumentException::invalidAssociation( + $this->em->getClassMetadata($assoc['targetEntity']), + $assoc, + $relatedEntities + ); + } + foreach ($relatedEntities as $relatedEntity) { $this->doPersist($relatedEntity, $visited); } + break; case ($relatedEntities !== null): + $targetClass = $this->em->getClassMetadata($assoc['targetEntity'])->name; + + if (! $relatedEntities instanceof $targetClass) { + throw ORMInvalidArgumentException::invalidAssociation( + $this->em->getClassMetadata($assoc['targetEntity']), + $assoc, + $relatedEntities + ); + } + $this->doPersist($relatedEntities, $visited); break;