Ver código fonte

[Bridge][Doctrine] Adding a catch for when a developer uses the EntityType with multiple=false but on a "hasMany" relationship

Without this, the exception is eventually thrown in EntityChoiceList::getIdentifierValues() with a very a message that doesn't really suit the real error: "Entities passed to the choice field must be managed"
Ryan Weaver 13 anos atrás
pai
commit
2270a4da5a

+ 5 - 0
src/Symfony/Bridge/Doctrine/Form/DataTransformer/EntityToIdTransformer.php

@@ -15,6 +15,7 @@ use Symfony\Bridge\Doctrine\Form\ChoiceList\EntityChoiceList;
 use Symfony\Component\Form\DataTransformerInterface;
 use Symfony\Component\Form\Exception\UnexpectedTypeException;
 use Symfony\Component\Form\Exception\TransformationFailedException;
+use Doctrine\Common\Collections\Collection;
 
 class EntityToIdTransformer implements DataTransformerInterface
 {
@@ -42,6 +43,10 @@ class EntityToIdTransformer implements DataTransformerInterface
             throw new UnexpectedTypeException($entity, 'object');
         }
 
+        if ($entity instanceof Collection) {
+            throw new \InvalidArgumentException('Expected an object, but got a collection. Did you forget to pass "multiple=true" to an entity field?');
+        }
+
         if (count($this->choiceList->getIdentifier()) > 1) {
             // load all choices
             $availableEntities = $this->choiceList->getEntities();