diff --git a/lib/Doctrine/ORM/ORMInvalidArgumentException.php b/lib/Doctrine/ORM/ORMInvalidArgumentException.php index cb50376797a..249b6b2469a 100644 --- a/lib/Doctrine/ORM/ORMInvalidArgumentException.php +++ b/lib/Doctrine/ORM/ORMInvalidArgumentException.php @@ -187,6 +187,28 @@ public static function invalidIdentifierBindingEntity() return new self("Binding entities to query parameters only allowed for entities that have an identifier."); } + /** + * @param object $relation + * @param string $fieldname + * @param mixed $value + * @return self + */ + public static function computeAssociationChangesError($relation, $fieldname, $value) + { + return new self(sprintf('Expected an Object for relation %s::%s got %s instead.', get_class($relation), $fieldname, gettype($value))); + } + + /** + * @param \Doctrine\ORM\Mapping\ClassMetadata $targetClass + * @param array $assoc + * @param mixed $entry + * @return self + */ + public static function invalidAssociation($targetClass, $assoc, $entry) + { + return new self(gettype($entry) . (is_scalar($entry) ? ' "'.$entry.'"': '') . ' is not an Object.'); + } + /** * Helper method to show an object as string. * diff --git a/lib/Doctrine/ORM/UnitOfWork.php b/lib/Doctrine/ORM/UnitOfWork.php index fd21c99ca2e..6df7f3d5aac 100644 --- a/lib/Doctrine/ORM/UnitOfWork.php +++ b/lib/Doctrine/ORM/UnitOfWork.php @@ -707,7 +707,11 @@ public function computeChangeSet(ClassMetadata $class, $entity) continue; } - $this->computeAssociationChanges($assoc, $val); + try { + $this->computeAssociationChanges($assoc, $val); + } catch (\Exception $ex) { + throw ORMInvalidArgumentException::computeAssociationChangesError($entity, $assoc['fieldName'], $ex->value); + } if ( ! isset($this->entityChangeSets[$oid]) && $assoc['isOwningSide'] && @@ -809,6 +813,10 @@ private function computeAssociationChanges($assoc, $value) $targetClass = $this->em->getClassMetadata($assoc['targetEntity']); foreach ($unwrappedValue as $key => $entry) { + if (! ($entry instanceof $targetClass->name)) { + throw ORMInvalidArgumentException::invalidAssociation($targetClass, $assoc, $entry); + } + $state = $this->getEntityState($entry, self::STATE_NEW); if ( ! ($entry instanceof $assoc['targetEntity'])) {