Jelajahi Sumber

Add support for many to many

Thomas Rabaix 13 tahun lalu
induk
melakukan
b87a9f6d7d

+ 7 - 7
Admin/BaseFieldDescription.php

@@ -35,8 +35,8 @@ use Sonata\AdminBundle\Admin\AdminInterface;
  *                           of the collection element.
  *
  * Form Field options :
- *   - form_field_type (o): the widget class to use to render the field
- *   - form_field_options (o): the options to give to the widget
+ *   - field_type (o): the widget class to use to render the field
+ *   - field_options (o): the options to give to the widget
  *   - edit (o) : list|inline|standard (only used for associated admin)
  *      - list : open a popup where the user can search, filter and click on one field
  *               to select one item
@@ -47,8 +47,9 @@ use Sonata\AdminBundle\Admin\AdminInterface;
  *   - identifier (o): if set to true a link appear on to edit the element
  *
  * Filter Field options :
- *   - filter_options (o): options given to the Filter object
- *   - filter_field_options (o): options given to the filter field object
+ *   - options (o): options given to the Filter object
+ *   - field_options (o): options given to the filter field object
+ *   - field_type (o): options given to the filter field object
  *
  * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
  */
@@ -395,8 +396,7 @@ abstract class BaseFieldDescription implements FieldDescriptionInterface
             $this->options[$name] = array();
         }
 
-        if (!is_array($this->options[$name]))
-        {
+        if (!is_array($this->options[$name])) {
             throw new \RuntimeException(sprintf('The key `%s` does not point to an array value', $name));
         }
 
@@ -444,7 +444,7 @@ abstract class BaseFieldDescription implements FieldDescriptionInterface
      */
     public static function camelize($property)
     {
-       return preg_replace(array('/(^|_| )+(.)/e', '/\.(.)/e'), array("strtoupper('\\2')", "'_'.strtoupper('\\1')"), $property);
+        return preg_replace(array('/(^|_| )+(.)/e', '/\.(.)/e'), array("strtoupper('\\2')", "'_'.strtoupper('\\1')"), $property);
     }
 
     public function setHelp($help)

+ 0 - 6
Builder/DatagridBuilderInterface.php

@@ -26,12 +26,6 @@ interface DatagridBuilderInterface
      */
     function fixFieldDescription(AdminInterface $admin, FieldDescriptionInterface $fieldDescription);
 
-    /**
-     * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
-     * @return array
-     */
-    function getChoices(FieldDescriptionInterface $fieldDescription);
-
     /**
      * @abstract
      * @param \Sonata\AdminBundle\Datagrid\DatagridInterface $datagrid

+ 9 - 37
Builder/ORM/DatagridBuilder.php

@@ -84,35 +84,6 @@ class DatagridBuilder implements DatagridBuilderInterface
         }
     }
 
-    /**
-     * @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
-     * @return array
-     */
-    public function getChoices(FieldDescriptionInterface $fieldDescription)
-    {
-        $modelManager = $fieldDescription->getAdmin()->getModelManager();
-        $targets = $modelManager->getEntityManager()
-            ->createQueryBuilder()
-            ->select('t')
-            ->from($fieldDescription->getTargetEntity(), 't')
-            ->getQuery()
-            ->execute();
-
-        $choices = array();
-        foreach ($targets as $target) {
-            // todo : puts this into a configuration option and use reflection
-            foreach (array('getTitle', 'getName', '__toString') as $getter) {
-                if (method_exists($target, $getter)) {
-                    $choices[$modelManager->getNormalizedIdentifier($target)] = $target->$getter();
-                    break;
-                }
-            }
-        }
-
-        return $choices;
-    }
-
-
     /**
      * @param \Sonata\AdminBundle\Datagrid\DatagridInterface $datagrid
      * @param null $type
@@ -127,18 +98,19 @@ class DatagridBuilder implements DatagridBuilderInterface
             $fieldDescription->setType($guessType->getType());
             $options = $guessType->getOptions();
 
-            $fieldDescription->setOption('options', $options['options']);
-            $fieldDescription->setOption('field_options', $options['field_options']);
-            $fieldDescription->setOption('field_type',    $options['field_type']);
+            $fieldDescription->setOption('options',       array_merge($options['options'], $fieldDescription->getOption('options', array())));
+            $fieldDescription->setOption('field_options', array_merge($options['field_options'], $fieldDescription->getOption('field_options', array())));
+            $fieldDescription->setOption('field_type',    $fieldDescription->getOption('field_type', $options['field_type']));
         } else {
             $fieldDescription->setType($type);
-            $options = array(
-                'options' => $fieldDescription->getOption('options', array()),
-                'field_options' => $fieldDescription->getOption('field_options', array()),
-                'field_type'    => $fieldDescription->getOption('field_type', array())
-            );
         }
 
+        $options = array(
+            'options'       => $fieldDescription->getOption('options', array()),
+            'field_options' => $fieldDescription->getOption('field_options', array()),
+            'field_type'    => $fieldDescription->getOption('field_type', array())
+        );
+
         $this->fixFieldDescription($admin, $fieldDescription);
         $admin->addFilterFieldDescription($fieldDescription->getName(), $fieldDescription);
 

+ 1 - 4
Filter/ORM/ChoiceFilter.php

@@ -33,10 +33,7 @@ class ChoiceFilter extends Filter
                 return;
             }
 
-            $queryBuilder->andWhere($queryBuilder->expr()->in(sprintf('%s.%s',
-                $alias,
-                $field
-            ), $value));
+            $queryBuilder->andWhere($queryBuilder->expr()->in(sprintf('%s.%s', $alias, $field ), $value));
         } else {
 
             if (empty($value) || $value == 'all') {

+ 1 - 1
Filter/ORM/Filter.php

@@ -28,7 +28,7 @@ abstract class Filter extends BaseFilter
 
     protected function association($queryBuilder, $value)
     {
-        if ($value && $this->getFieldDescription()->getType() == ClassMetadataInfo::MANY_TO_MANY) {
+        if ($value && $this->getFieldDescription()->getMappingType() == ClassMetadataInfo::MANY_TO_MANY) {
             $queryBuilder->leftJoin(
                 sprintf('%s.%s', $queryBuilder->getRootAlias(), $this->getFieldDescription()->getFieldName()),
                 $this->getName()

+ 15 - 10
Guesser/ORM/FilterTypeGuesser.php

@@ -55,16 +55,21 @@ class FilterTypeGuesser implements TypeGuesserInterface
 
             switch ($mapping['type']) {
                 case ClassMetadataInfo::ONE_TO_MANY:
-                    return new TypeGuess('orm_one_to_many', $options, Guess::HIGH_CONFIDENCE);
+                    return new TypeGuess('doctrine_orm_one_to_many', $options, Guess::HIGH_CONFIDENCE);
 
                 case ClassMetadataInfo::MANY_TO_MANY:
-                    return new TypeGuess('orm_many_to_many', $options, Guess::HIGH_CONFIDENCE);
+                    $options['field_type'] = 'entity';
+                    $options['field_options'] = array(
+                        'class' => $class
+                    );
+
+                    return new TypeGuess('doctrine_orm_choice', $options, Guess::HIGH_CONFIDENCE);
 
                 case ClassMetadataInfo::MANY_TO_ONE:
-                    return new TypeGuess('orm_many_to_one', $options, Guess::HIGH_CONFIDENCE);
+                    return new TypeGuess('doctrine_orm_many_to_one', $options, Guess::HIGH_CONFIDENCE);
 
                 case ClassMetadataInfo::ONE_TO_ONE:
-                    return new TypeGuess('orm_one_to_one', $options, Guess::HIGH_CONFIDENCE);
+                    return new TypeGuess('doctrine_orm_one_to_one', $options, Guess::HIGH_CONFIDENCE);
             }
         }
 
@@ -76,12 +81,12 @@ class FilterTypeGuesser implements TypeGuesserInterface
                 $options['field_options'] = array();
 
                 return new TypeGuess('doctrine_orm_boolean', $options, Guess::HIGH_CONFIDENCE);
-            case 'datetime':
-            case 'vardatetime':
-            case 'datetimetz':
-                return new TypeGuess('doctrine_orm_datetime', $options, Guess::HIGH_CONFIDENCE);
-            case 'date':
-                return new TypeGuess('doctrine_orm_date', $options, Guess::HIGH_CONFIDENCE);
+//            case 'datetime':
+//            case 'vardatetime':
+//            case 'datetimetz':
+//                return new TypeGuess('doctrine_orm_datetime', $options, Guess::HIGH_CONFIDENCE);
+//            case 'date':
+//                return new TypeGuess('doctrine_orm_date', $options, Guess::HIGH_CONFIDENCE);
             case 'decimal':
             case 'float':
                 return new TypeGuess('doctrine_orm_number', $options, Guess::MEDIUM_CONFIDENCE);