Browse Source

Merge pull request #3655 from Mangetsu/sf3-model-loader-patch

Fix ChoiceLoader issue SF2.7+
Oskar Stark 9 years ago
parent
commit
c42f1d6a41
2 changed files with 15 additions and 6 deletions
  1. 11 5
      Form/DataTransformer/ModelsToArrayTransformer.php
  2. 4 1
      Form/Type/ModelType.php

+ 11 - 5
Form/DataTransformer/ModelsToArrayTransformer.php

@@ -12,6 +12,7 @@
 namespace Sonata\AdminBundle\Form\DataTransformer;
 namespace Sonata\AdminBundle\Form\DataTransformer;
 
 
 use Sonata\AdminBundle\Form\ChoiceList\ModelChoiceList;
 use Sonata\AdminBundle\Form\ChoiceList\ModelChoiceList;
+use Sonata\AdminBundle\Form\ChoiceList\ModelChoiceLoader;
 use Sonata\AdminBundle\Model\ModelManagerInterface;
 use Sonata\AdminBundle\Model\ModelManagerInterface;
 use Symfony\Component\Form\ChoiceList\LazyChoiceList;
 use Symfony\Component\Form\ChoiceList\LazyChoiceList;
 use Symfony\Component\Form\DataTransformerInterface;
 use Symfony\Component\Form\DataTransformerInterface;
@@ -44,14 +45,19 @@ class ModelsToArrayTransformer implements DataTransformerInterface
     /**
     /**
      * ModelsToArrayTransformer constructor.
      * ModelsToArrayTransformer constructor.
      *
      *
-     * @param ModelChoiceList|LazyChoiceList $choiceList
-     * @param ModelManagerInterface          $modelManager
-     * @param                                $class
+     * @param ModelChoiceList|LazyChoiceList|ModelChoiceLoader $choiceList
+     * @param ModelManagerInterface                            $modelManager
+     * @param $class
+     *
+     * @throws RuntimeException
      */
      */
     public function __construct($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');
+        if (!$choiceList instanceof ModelChoiceList
+            && !$choiceList instanceof ModelChoiceLoader
+            && !$choiceList instanceof LazyChoiceList) {
+            throw new RuntimeException('First param passed to ModelsToArrayTransformer should be instance of
+                ModelChoiceLoader or ModelChoiceList or LazyChoiceList');
         }
         }
 
 
         $this->choiceList   = $choiceList;
         $this->choiceList   = $choiceList;

+ 4 - 1
Form/Type/ModelType.php

@@ -51,7 +51,10 @@ class ModelType extends AbstractType
     {
     {
         if ($options['multiple']) {
         if ($options['multiple']) {
             if (array_key_exists('choice_loader', $options) && $options['choice_loader'] !== null) { // SF2.7+
             if (array_key_exists('choice_loader', $options) && $options['choice_loader'] !== null) { // SF2.7+
-                $builder->addViewTransformer(new ModelsToArrayTransformer($options['choice_list'], $options['model_manager'], $options['class']), true);
+                $builder->addViewTransformer(new ModelsToArrayTransformer(
+                    $options['choice_loader'],
+                    $options['model_manager'],
+                    $options['class']), true);
             } else {
             } else {
                 $builder->addViewTransformer(new LegacyModelsToArrayTransformer($options['choice_list']), true);
                 $builder->addViewTransformer(new LegacyModelsToArrayTransformer($options['choice_list']), true);
             }
             }