Browse Source

fixed merging bugs

r1pp3rj4ck 13 năm trước cách đây
mục cha
commit
106e96a94d

+ 2 - 1
.gitignore

@@ -6,4 +6,5 @@ Resources/doc/_build/*
 nbproject
 coverage
 composer.lock
-vendor
+vendor
+composer.phar

+ 57 - 0
Admin/BaseFieldDescription.php

@@ -314,6 +314,14 @@ abstract class BaseFieldDescription implements FieldDescriptionInterface
         return $this->fieldMapping;
     }
 
+    /**
+     * {@inheritdoc}
+     */
+    public function getParentAssociationMappings()
+    {
+        return $this->parentAssociationMappings;
+    }
+
     /**
      * set the association admin instance (only used if the field is linked to an Admin)
      *
@@ -343,6 +351,31 @@ abstract class BaseFieldDescription implements FieldDescriptionInterface
         return $this->associationAdmin !== null;
     }
 
+    /**
+     * {@inheritdoc}
+     */
+    public function getFieldValue($object, $fieldName)
+    {
+        $camelizedFieldName = self::camelize($fieldName);
+
+        $getters = array();
+        // prefer method name given in the code option
+        if ($this->getOption('code')) {
+            $getters[] = $this->getOption('code');
+        }
+        $getters[] = 'get'.$camelizedFieldName;
+        $getters[] = 'is'.$camelizedFieldName;
+
+
+        foreach ($getters as $getter) {
+            if (method_exists($object, $getter)) {
+                return call_user_func(array($object, $getter));
+            }
+        }
+
+        throw new NoValueException(sprintf('Unable to retrieve the value of `%s`', $this->getName()));
+    }
+
     /**
      * return the value linked to the description
      *
@@ -482,4 +515,28 @@ abstract class BaseFieldDescription implements FieldDescriptionInterface
     {
         return $this->getOption('label');
     }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function isSortable()
+    {
+        return $this->getOption('sortable', false);
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function getSortFieldMapping()
+    {
+        return $this->getOption('sort_field_mapping');
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function getSortParentAssociationMapping()
+    {
+        return $this->getOption('sort_parent_association_mappings');
+    }
 }

+ 16 - 0
Admin/FieldDescriptionInterface.php

@@ -166,6 +166,22 @@ interface FieldDescriptionInterface
      */
     function getFieldMapping();
 
+    /**
+     * set the parent association mappings information
+     *
+     * @param array $parentAssociationMappings
+     *
+     * @return void
+     */
+    function setParentAssociationMappings(array $parentAssociationMappings);
+
+    /**
+     * return the parent association mapping definitions
+     *
+     * @return array the parent association mapping definitions
+     */
+    function getParentAssociationMappings();
+
     /**
      * set the association admin instance (only used if the field is linked to an Admin)
      *

+ 10 - 2
Datagrid/Datagrid.php

@@ -110,8 +110,16 @@ class Datagrid implements DatagridInterface
             $filter->apply($this->query, $data[$name]);
         }
 
-        $this->query->setSortBy(isset($this->values['_sort_by']) ? $this->values['_sort_by'] : null);
-        $this->query->setSortOrder(isset($this->values['_sort_order']) ? $this->values['_sort_order'] : null);
+        if (isset($this->values['_sort_by'])) {
+            foreach ($this->getColumns()->getElements() as $fieldDescription){
+                if ($fieldDescription->isSortable() && $fieldDescription->getName() == $this->values['_sort_by']) {
+                    $this->query->setSortBy($fieldDescription->getParentAssociationMappings(), $fieldDescription->getSortFieldMapping());
+                    $this->query->setSortOrder(isset($this->values['_sort_order']) ? $this->values['_sort_order'] : null);
+
+                    break;
+                }
+            }
+        }
 
         $this->pager->setPage(isset($this->values['_page']) ? $this->values['_page'] : 1);
         $this->pager->setQuery($this->query);

+ 1 - 1
Datagrid/ProxyQueryInterface.php

@@ -20,7 +20,7 @@ interface ProxyQueryInterface
 
     function __call($name, $args);
 
-    function setSortBy($sortBy);
+    function setSortBy($parentAssociationMappings, $fieldMapping);
 
     function getSortBy();
 

+ 36 - 0
Filter/Filter.php

@@ -114,6 +114,42 @@ abstract class Filter implements FilterInterface
         return $fieldName;
     }
 
+    /**
+     * {@inheritdoc}
+     */
+    public function getParentAssociationMappings()
+    {
+        return $this->getOption('parent_association_mappings', array());
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function getFieldMapping()
+    {
+        $fieldMapping = $this->getOption('field_mapping');
+
+        if (!$fieldMapping) {
+            throw new \RunTimeException(sprintf('The option `field_mapping` must be set for field : `%s`', $this->getName()));
+        }
+
+        return $fieldMapping;
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function getAssociationMapping()
+    {
+        $associationMapping = $this->getOption('association_mapping');
+
+        if (!$associationMapping) {
+            throw new \RunTimeException(sprintf('The option `association_mapping` must be set for field : `%s`', $this->getName()));
+        }
+
+        return $associationMapping;
+    }
+
     /**
      * @param array $options
      * @return void

+ 3 - 2
Filter/FilterInterface.php

@@ -10,6 +10,7 @@
  */
 
 namespace Sonata\AdminBundle\Filter;
+use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
 
 interface FilterInterface
 {
@@ -17,13 +18,13 @@ interface FilterInterface
      * Apply the filter to the QueryBuilder instance
      *
      * @abstract
-     * @param $queryBuilder
+     * @param ProxyQueryInterface $queryBuilder
      * @param string $alias
      * @param string $field
      * @param string $value
      * @return void
      */
-    function filter($queryBuilder, $alias, $field, $value);
+    function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $value);
 
     /**
      * @abstract

+ 5 - 3
Guesser/TypeGuesserChain.php

@@ -15,6 +15,7 @@ namespace Sonata\AdminBundle\Guesser;
 use Sonata\AdminBundle\Guesser\TypeGuesserInterface;
 use Symfony\Component\Form\Exception\UnexpectedTypeException;
 use Symfony\Component\Form\Guess\Guess;
+use Sonata\AdminBundle\Model\ModelManagerInterface;
 
 /**
  *
@@ -45,12 +46,13 @@ class TypeGuesserChain implements TypeGuesserInterface
     /**
      * @param $class
      * @param $property
+     * @param \Sonata\AdminBundle\Model\ModelManagerInterface $modelManager
      * @return FieldFactoryGuess
      */
-    public function guessType($class, $property)
+    public function guessType($class, $property, ModelManagerInterface $modelManager)
     {
-        return $this->guess(function ($guesser) use ($class, $property) {
-            return $guesser->guessType($class, $property);
+        return $this->guess(function ($guesser) use ($class, $property, $modelManager) {
+            return $guesser->guessType($class, $property, $modelManager);
         });
     }
 

+ 4 - 1
Guesser/TypeGuesserInterface.php

@@ -12,13 +12,16 @@
 
 namespace Sonata\AdminBundle\Guesser;
 
+use Sonata\AdminBundle\Model\ModelManagerInterface;
+
 interface TypeGuesserInterface
 {
     /**
      * @abstract
      * @param string $class
      * @param string $property
+     * @param \Sonata\AdminBundle\Model\ModelManagerInterface $modelManager
      * @return TypeGuess
      */
-    function guessType($class, $property);
+    function guessType($class, $property, ModelManagerInterface $modelManager);
 }

+ 2 - 1
Tests/Filter/FilterTest.php

@@ -12,10 +12,11 @@
 namespace Sonata\AdminBundle\Tests\Filter;
 
 use Sonata\AdminBundle\Filter\Filter;
+use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
 
 class FilterTest_Filter extends Filter
 {
-    function filter($queryBuilder, $alias, $field, $value)
+    function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $value)
     {
     }