瀏覽代碼

Updated logic and added Exception messages when an incorrect configuration is used.

caponica 10 年之前
父節點
當前提交
d3ce16f857
共有 1 個文件被更改,包括 15 次插入5 次删除
  1. 15 5
      Form/DataTransformer/ModelToIdPropertyTransformer.php

+ 15 - 5
Form/DataTransformer/ModelToIdPropertyTransformer.php

@@ -84,12 +84,22 @@ class ModelToIdPropertyTransformer implements DataTransformerInterface
         if (!$entityOrCollection) {
             return $result;
         }
-        if (substr(get_class($entityOrCollection), -1 * strlen($this->className)) == $this->className) {
-            $collection = array($entityOrCollection);
-        } elseif ($entityOrCollection instanceof \ArrayAccess) {
-            $collection = $entityOrCollection;
+        if ($this->multiple) {
+            if (substr(get_class($entityOrCollection), -1 * strlen($this->className)) == $this->className) {
+                throw new \InvalidArgumentException('A multiple selection must be passed a collection not a single value.');
+            } elseif ($entityOrCollection instanceof \ArrayAccess) {
+                $collection = $entityOrCollection;
+            } else {
+                throw new \InvalidArgumentException('A multiple selection must be passed a collection not a single value.');
+            }
         } else {
-            $collection = array($entityOrCollection);
+            if (substr(get_class($entityOrCollection), -1 * strlen($this->className)) == $this->className) {
+                $collection = array($entityOrCollection);
+            } elseif ($entityOrCollection instanceof \ArrayAccess) {
+                throw new \InvalidArgumentException('A single selection must be passed a single value not a collection.');
+            } else {
+                $collection = array($entityOrCollection);
+            }
         }
 
         if (empty($this->property)) {