Selaa lähdekoodia

Fix nested admin list filtering, add ModelReferenceType

Thomas Rabaix 13 vuotta sitten
vanhempi
commit
789802d1a8

+ 6 - 4
Admin/Admin.php

@@ -589,10 +589,12 @@ abstract class Admin implements AdminInterface, DomainObjectInterface
 
         // ok, try to limit to add parent filter
         if ($this->getParentAssociationMapping()) {
-            $fieldDescription = $this->getModelManager()->getParentFieldDescription($this->getParentAssociationMapping(), $this->getClass());
-            $this->filterFieldDescriptions[$this->getParentAssociationMapping()] = $fieldDescription;
-
-            $mapper->add($fieldDescription);
+            $mapper->add($this->getParentAssociationMapping(), null, array(
+                'field_type' => 'sonata_type_model_reference',
+                'field_options' => array(
+                    'model_manager' => $this->getModelManager()
+                )
+            ));
         }
     }
 

+ 7 - 1
Builder/ORM/DatagridBuilder.php

@@ -88,7 +88,13 @@ class DatagridBuilder implements DatagridBuilderInterface
             $fieldDescription->setType($guessType->getType());
             $options = $guessType->getOptions();
 
-            $fieldDescription->mergeOptions($options);
+            foreach($options as $name => $value) {
+                if (is_array($value)) {
+                    $fieldDescription->setOption($name, array_merge($value, $fieldDescription->getOption($name, array())));
+                } else {
+                    $fieldDescription->setOption($name,    $fieldDescription->getOption($name, $value));
+                }
+            }
         } else {
             $fieldDescription->setType($type);
         }

+ 0 - 1
Builder/ORM/FormContractor.php

@@ -94,7 +94,6 @@ class FormContractor implements FormContractorInterface
     /**
      * @param $type
      * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
-     * @param array $options
      * @return array
      */
     public function getDefaultOptions($type, FieldDescriptionInterface $fieldDescription)

+ 1 - 2
Filter/ORM/ModelFilter.php

@@ -57,8 +57,7 @@ class ModelFilter extends Filter
 
         $queryBuilder->leftJoin(sprintf('%s.%s', $queryBuilder->getRootAlias(), $this->getOption('field_name')), $this->getName());
 
-        // todo : use the metadata information to find the correct column name
-        return array($this->getName(), 'id');
+        return array($this->getOption('field_name'), 'id');
     }
 
     public function getDefaultOptions()

+ 56 - 0
Form/Type/ModelReferenceType.php

@@ -0,0 +1,56 @@
+<?php
+
+/*
+ * This file is part of the Sonata package.
+ *
+ * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ *
+ */
+
+namespace Sonata\AdminBundle\Form\Type;
+
+use Symfony\Component\Form\FormBuilder;
+use Symfony\Component\Form\FormFactoryInterface;
+use Symfony\Component\Form\AbstractType;
+use Symfony\Component\Form\FormInterface;
+use Symfony\Component\Form\FormView;
+
+use Sonata\AdminBundle\Form\EventListener\MergeCollectionListener;
+use Sonata\AdminBundle\Form\ChoiceList\ModelChoiceList;
+use Sonata\AdminBundle\Form\DataTransformer\ModelsToArrayTransformer;
+use Sonata\AdminBundle\Form\DataTransformer\ModelToIdTransformer;
+use Sonata\AdminBundle\Model\ModelManagerInterface;
+
+class ModelReferenceType extends AbstractType
+{
+    public function buildForm(FormBuilder $builder, array $options)
+    {
+        $builder->prependClientTransformer(new ModelToIdTransformer($options['model_manager'], $options['class']));
+    }
+
+    public function getDefaultOptions(array $options)
+    {
+        $defaultOptions = array(
+            'model_manager'     => null,
+            'class'             => null,
+            'parent'            => 'hidden',
+        );
+
+        $options = array_replace($defaultOptions, $options);
+
+        return $options;
+    }
+
+    public function getParent(array $options)
+    {
+        return $options['parent'];
+    }
+
+    public function getName()
+    {
+        return 'sonata_type_model_reference';
+    }
+}

+ 5 - 0
Resources/config/form_types.xml

@@ -19,10 +19,15 @@
             <tag name="form.type" alias="sonata_type_model" />
         </service>
 
+        <service id="sonata.admin.form.type.model_reference" class="Sonata\AdminBundle\Form\Type\ModelReferenceType">
+            <tag name="form.type" alias="sonata_type_model_reference" />
+        </service>
+
         <service id="sonata.admin.form.type.array" class="Sonata\AdminBundle\Form\Type\ImmutableArrayType">
             <tag name="form.type" alias="sonata_type_immutable_array" />
         </service>
 
+        <!-- Form Extension -->
         <service id="sonata.admin.form.extension.field" class="Sonata\AdminBundle\Form\Extension\Field\Type\FormTypeFieldExtension">
             <tag name="form.type_extension" alias="field" />
         </service>

+ 7 - 0
Tests/Admin/FieldDescriptionTest.php

@@ -45,6 +45,12 @@ class FieldDescriptionTest extends \PHPUnit_Framework_TestCase
         $field->mergeOption('non_existant', array('key1' => 'key_1', 'key2' => 'key_2'));
         $this->assertEquals(array('key1' => 'key_1', 'key2' => 'key_2'), $field->getOption('array'));
 
+
+        $field->mergeOptions(array('array' => array('key3' => 'key_3')));
+
+        $this->assertEquals(array('key1' => 'key_1', 'key2' => 'key_2', 'key3' => 'key_3'), $field->getOption('array'));
+
+
         $field->setOption('integer', 1);
         try {
             $field->mergeOption('integer', array());
@@ -61,6 +67,7 @@ class FieldDescriptionTest extends \PHPUnit_Framework_TestCase
           array (
             'key1' => 'key_1',
             'key2' => 'key_2',
+            'key3' => 'key_3'
           ),
           'non_existant' =>
           array (