Thomas Rabaix пре 13 година
родитељ
комит
0a4b7ef052

+ 16 - 24
Form/ChoiceList/ModelChoiceList.php

@@ -73,11 +73,11 @@ class ModelChoiceList extends SimpleChoiceList
     private $propertyPath;
 
     /**
-     * @param \Sonata\AdminBundle\Model\ModelManagerInterface $modelManager
-     * @param string                                          $class
-     * @param null                                            $property
-     * @param null                                            $query
-     * @param array                                           $choices
+     * @param ModelManagerInterface $modelManager
+     * @param string                $class
+     * @param null                  $property
+     * @param null                  $query
+     * @param array                 $choices
      */
     public function __construct(ModelManagerInterface $modelManager, $class, $property = null, $query = null, $choices = array())
     {
@@ -92,9 +92,7 @@ class ModelChoiceList extends SimpleChoiceList
             $this->propertyPath = new PropertyPath($property);
         }
 
-        $this->choices = $choices;
-        $this->load();
-        parent::__construct($this->choices);
+        parent::__construct($this->load($choices));
     }
 
     /**
@@ -116,17 +114,17 @@ class ModelChoiceList extends SimpleChoiceList
      *
      * @return array  An array of choices
      */
-    protected function load()
+    protected function load($choices)
     {
-        if (is_array($this->choices)) {
-            $entities = $this->choices;
+        if (is_array($choices)) {
+            $entities = $choices;
         } else if ($this->query) {
             $entities = $this->modelManager->executeQuery($this->query);
         } else {
             $entities = $this->modelManager->findBy($this->class);
         }
 
-        $this->choices = array();
+        $choices = array();
         $this->entities = array();
 
         foreach ($entities as $key => $entity) {
@@ -141,16 +139,18 @@ class ModelChoiceList extends SimpleChoiceList
             if (count($this->identifier) > 1) {
                 // When the identifier consists of multiple field, use
                 // naturally ordered keys to refer to the choices
-                $this->choices[$key] = $value;
+                $choices[$key] = $value;
                 $this->entities[$key] = $entity;
             } else {
                 // When the identifier is a single field, index choices by
                 // entity ID for performance reasons
                 $id = current($this->getIdentifierValues($entity));
-                $this->choices[$id] = $value;
+                $choices[$id] = $value;
                 $this->entities[$id] = $entity;
             }
         }
+
+        return $choices;
     }
 
     /**
@@ -192,6 +192,7 @@ class ModelChoiceList extends SimpleChoiceList
      */
     public function getEntity($key)
     {
+
         if (count($this->identifier) > 1) {
             // $key is a collection index
             $entities = $this->getEntities();
@@ -200,15 +201,6 @@ class ModelChoiceList extends SimpleChoiceList
             return isset($this->entities[$key]) ? $this->entities[$key] : null;
         }
 
-          // todo : I don't see the point of this ..
-//            else if ($qb = $this->queryBuilder) {
-//                // should we clone the builder?
-//                $alias = $qb->getRootAlias();
-//                $where = $qb->expr()->eq($alias.'.'.current($this->identifier), $key);
-//
-//                return $qb->andWhere($where)->getQuery()->getSingleResult();
-//            }
-
         return $this->modelManager->find($this->class, $key);
     }
 
@@ -259,6 +251,6 @@ class ModelChoiceList extends SimpleChoiceList
      */
     public function getClass()
     {
-      return $this->class;
+        return $this->class;
     }
 }

+ 2 - 2
Form/DataTransformer/ModelToIdTransformer.php

@@ -30,8 +30,8 @@ class ModelToIdTransformer implements DataTransformerInterface
     protected $className;
 
     /**
-     * @param \Sonata\AdminBundle\Model\ModelManagerInterface $modelManager
-     * @param string                                          $className
+     * @param ModelManagerInterface $modelManager
+     * @param string                $className
      */
     public function __construct(ModelManagerInterface $modelManager, $className)
     {

+ 6 - 1
Form/EventListener/MergeCollectionListener.php

@@ -35,7 +35,9 @@ class MergeCollectionListener implements EventSubscriberInterface
      */
     public static function getSubscribedEvents()
     {
-        return array(FormEvents::BIND => 'onBind');
+        return array(
+            FormEvents::BIND => array('onBind', 10),
+        );
     }
 
     /**
@@ -46,6 +48,9 @@ class MergeCollectionListener implements EventSubscriberInterface
         $collection = $event->getForm()->getData();
         $data       = $event->getData();
 
+        // looks like there is no way to remove other listeners
+        $event->stopPropagation();
+
         if (!$collection) {
             $collection = $data;
         } else if (count($data) === 0) {

+ 14 - 8
Form/Type/ModelType.php

@@ -27,6 +27,10 @@ use Sonata\AdminBundle\Form\DataTransformer\ModelsToArrayTransformer;
 use Sonata\AdminBundle\Form\DataTransformer\ModelToIdTransformer;
 use Sonata\AdminBundle\Model\ModelManagerInterface;
 
+/**
+ * This type define a standard select input with a + sign to add new associated object
+ *
+ */
 class ModelType extends AbstractType
 {
     /**
@@ -37,19 +41,24 @@ class ModelType extends AbstractType
         if ($options['multiple']) {
             $builder
                 ->addEventSubscriber(new MergeCollectionListener($options['model_manager']))
-                ->prependClientTransformer(new ModelsToArrayTransformer($options['choice_list']));
+                ->addViewTransformer(new ModelsToArrayTransformer($options['choice_list']), true);
         } else {
-            $builder->prependClientTransformer(new ModelToIdTransformer($options['model_manager'], $options['class']));
+            $builder
+                ->addViewTransformer(new ModelToIdTransformer($options['model_manager'], $options['class']), true)
+            ;
         }
     }
 
+    /**
+     * {@inheritDoc}
+     */
     public function setDefaultOptions(OptionsResolverInterface $resolver)
     {
         $resolver->setDefaults(array(
             'compound'          => function (Options $options) {
-                return isset($options['parent']) ? $options['parent'] : 'choice';
+                return isset($options['multiple']) ? $options['multiple'] : false;
             },
-                    
+
             'template'          => 'choice',
             'multiple'          => false,
             'expanded'          => false,
@@ -58,12 +67,9 @@ class ModelType extends AbstractType
             'property'          => null,
             'query'             => null,
             'choices'           => null,
-            'parent'            => 'choice',
             'preferred_choices' => array(),
-            
             'choice_list'       => function (Options $options, $previousValue) {
-                if ($previousValue instanceof ChoiceListInterface
-                        && count($choices = $previousValue->getChoices())) {
+                if ($previousValue instanceof ChoiceListInterface && count($choices = $previousValue->getChoices())) {
                     return $choices;
                 }
 

+ 13 - 0
UPGRATE-2.1.md

@@ -0,0 +1,13 @@
+UPGRADE FROM 2.0 to 2.1
+=======================
+
+### Form
+
+  * Due to some refactoring in the Form Component, some types definition have been changed
+    and new ones have been introduces:
+
+      * sonata_type_model : this type now only render a standard select widget or a list
+        widget (if multiple option is set)
+
+
+