Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improved error handling for invalid association values #2 #1248

Closed
wants to merge 10 commits into from
22 changes: 22 additions & 0 deletions lib/Doctrine/ORM/ORMInvalidArgumentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
10 changes: 9 additions & 1 deletion lib/Doctrine/ORM/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,11 @@ public function computeChangeSet(ClassMetadata $class, $entity)
continue;
}

$this->computeAssociationChanges($assoc, $val);
try {
$this->computeAssociationChanges($assoc, $val);
} catch (\Exception $ex) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't catch a generic exception here: be specific

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how i can track all the possible situations which yield all possible Exceptions

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@flip111 writing these tests isn't that problematic if you keep the test asset as simple as possible.

throw ORMInvalidArgumentException::computeAssociationChangesError($entity, $assoc['fieldName'], $ex->value);
}

if ( ! isset($this->entityChangeSets[$oid]) &&
$assoc['isOwningSide'] &&
Expand Down Expand Up @@ -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'])) {
Expand Down