瀏覽代碼

merged branch weaverryan/entity_better_exception_2_0 (PR #2372)

Commits
-------

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

Discussion
----------

[Bridge][Doctrine] Adding a catch for when a developer uses the EntityType

Hey guys!

See #2348 - same commit, but against the right branch.

Thanks!
Fabien Potencier 13 年之前
父節點
當前提交
550f158428
共有 1 個文件被更改,包括 5 次插入0 次删除
  1. 5 0
      src/Symfony/Bridge/Doctrine/Form/DataTransformer/EntityToIdTransformer.php

+ 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();