Browse Source

Merge pull request #3504 from qsomazzi/model_choice_improve

Improve ModelChoiceLoader
Thomas 9 years ago
parent
commit
18c495e1f2

+ 6 - 6
Form/ChoiceList/ModelChoiceLoader.php

@@ -77,10 +77,10 @@ class ModelChoiceLoader implements ChoiceLoaderInterface
     public function loadChoiceList($value = null)
     public function loadChoiceList($value = null)
     {
     {
         if (!$this->choiceList) {
         if (!$this->choiceList) {
-            if (is_array($this->choices)) {
-                $entities = $this->choices;
-            } elseif ($this->query) {
+            if ($this->query) {
                 $entities = $this->modelManager->executeQuery($this->query);
                 $entities = $this->modelManager->executeQuery($this->query);
+            } elseif (is_array($this->choices)) {
+                $entities = $this->choices;
             } else {
             } else {
                 $entities = $this->modelManager->findBy($this->class);
                 $entities = $this->modelManager->findBy($this->class);
             }
             }
@@ -129,9 +129,9 @@ class ModelChoiceLoader implements ChoiceLoaderInterface
     /**
     /**
      * {@inheritdoc}
      * {@inheritdoc}
      */
      */
-    public function loadChoicesForValues(array $choices, $value = null)
+    public function loadChoicesForValues(array $values, $value = null)
     {
     {
-        throw new \RuntimeException('Not implemented, please send us your usecase');
+        return $this->loadChoiceList($value)->getChoicesForValues($values);
     }
     }
 
 
     /**
     /**
@@ -139,7 +139,7 @@ class ModelChoiceLoader implements ChoiceLoaderInterface
      */
      */
     public function loadValuesForChoices(array $choices, $value = null)
     public function loadValuesForChoices(array $choices, $value = null)
     {
     {
-        throw new \RuntimeException('Not implemented, please send us your usecase');
+        return $this->loadChoiceList($value)->getValuesForChoices($choices);
     }
     }
 
 
     /**
     /**

+ 10 - 4
Form/DataTransformer/ModelsToArrayTransformer.php

@@ -13,7 +13,9 @@ namespace Sonata\AdminBundle\Form\DataTransformer;
 
 
 use Sonata\AdminBundle\Form\ChoiceList\ModelChoiceList;
 use Sonata\AdminBundle\Form\ChoiceList\ModelChoiceList;
 use Sonata\AdminBundle\Model\ModelManagerInterface;
 use Sonata\AdminBundle\Model\ModelManagerInterface;
+use Symfony\Component\Form\ChoiceList\LazyChoiceList;
 use Symfony\Component\Form\DataTransformerInterface;
 use Symfony\Component\Form\DataTransformerInterface;
+use Symfony\Component\Form\Exception\RuntimeException;
 use Symfony\Component\Form\Exception\TransformationFailedException;
 use Symfony\Component\Form\Exception\TransformationFailedException;
 use Symfony\Component\Form\Exception\UnexpectedTypeException;
 use Symfony\Component\Form\Exception\UnexpectedTypeException;
 
 
@@ -42,12 +44,16 @@ class ModelsToArrayTransformer implements DataTransformerInterface
     /**
     /**
      * ModelsToArrayTransformer constructor.
      * ModelsToArrayTransformer constructor.
      *
      *
-     * @param ModelChoiceList       $choiceList
-     * @param ModelManagerInterface $modelManager
-     * @param                       $class
+     * @param ModelChoiceList|LazyChoiceList $choiceList
+     * @param ModelManagerInterface          $modelManager
+     * @param                                $class
      */
      */
-    public function __construct(ModelChoiceList $choiceList, ModelManagerInterface $modelManager, $class)
+    public function __construct($choiceList, ModelManagerInterface $modelManager, $class)
     {
     {
+        if (!$choiceList instanceof ModelChoiceList && !$choiceList instanceof LazyChoiceList) {
+            throw new RuntimeException('First param passed to ModelsToArrayTransformer should be instance of ModelChoiceList or LazyChoiceList');
+        }
+
         $this->choiceList   = $choiceList;
         $this->choiceList   = $choiceList;
         $this->modelManager = $modelManager;
         $this->modelManager = $modelManager;
         $this->class        = $class;
         $this->class        = $class;