Romain Geissler преди 13 години
родител
ревизия
0a1f317998
променени са 5 файла, в които са добавени 96 реда и са изтрити 9 реда
  1. 49 5
      Admin/BaseFieldDescription.php
  2. 34 0
      Admin/FieldDescriptionInterface.php
  3. 11 2
      Datagrid/Datagrid.php
  4. 1 1
      Datagrid/ProxyQueryInterface.php
  5. 1 1
      Resources/views/CRUD/base_list.html.twig

+ 49 - 5
Admin/BaseFieldDescription.php

@@ -77,15 +77,20 @@ abstract class BaseFieldDescription implements FieldDescriptionInterface
     protected $fieldName;
 
     /**
-     * @var array the Doctrine association mapping
+     * @var array the ORM association mapping
      */
     protected $associationMapping;
 
     /**
-     * @var array the Doctrine field information
+     * @var array the ORM field information
      */
     protected $fieldMapping;
 
+    /*
+     * var array the ORM parent mapping association
+     */
+    protected $parentAssociationMappings;
+
     /**
      * @var string the template name
      */
@@ -148,7 +153,7 @@ abstract class BaseFieldDescription implements FieldDescriptionInterface
         $this->name = $name;
 
         if (!$this->getFieldName()) {
-            $this->setFieldName($name);
+            $this->setFieldName(substr(strrchr('.'.$name, '.') ,1));
         }
     }
 
@@ -314,6 +319,16 @@ abstract class BaseFieldDescription implements FieldDescriptionInterface
         return $this->fieldMapping;
     }
 
+    /**
+     * return the parent association mapping definitions
+     *
+     * @return array the parent association mapping definitions
+     */
+    public function getParentAssociationMappings()
+    {
+        return $this->parentAssociationMappings;
+    }
+
     /**
      * set the association admin instance (only used if the field is linked to an Admin)
      *
@@ -347,11 +362,12 @@ abstract class BaseFieldDescription implements FieldDescriptionInterface
      * return the value linked to the description
      *
      * @param  $object
+     * @param string $fieldName
      * @return bool|mixed
      */
-    public function getValue($object)
+    public function getFieldValue($object, $fieldName)
     {
-        $camelizedFieldName = self::camelize($this->getFieldName());
+        $camelizedFieldName = self::camelize($fieldName);
 
         $getters = array();
         // prefer method name given in the code option
@@ -482,4 +498,32 @@ abstract class BaseFieldDescription implements FieldDescriptionInterface
     {
         return $this->getOption('label');
     }
+
+    /**
+     * @return boolean
+     */
+    public function isSortable()
+    {
+        return $this->getOption('sortable', false);
+    }
+
+    /**
+     * return the field mapping definition used when sorting
+     *
+     * @return array the field mapping definition
+     */
+    public function getSortFieldMapping()
+    {
+        return $this->getOption('sort_field_mapping');
+    }
+
+    /**
+     * return the parent association mapping definitions used when sorting
+     *
+     * @return array the parent association mapping definitions
+     */
+    public function getSortParentAssociationMapping()
+    {
+        return $this->getOption('sort_parent_association_mappings');
+    }
 }

+ 34 - 0
Admin/FieldDescriptionInterface.php

@@ -166,6 +166,21 @@ interface FieldDescriptionInterface
      */
     function getFieldMapping();
 
+    /**
+     * set the parent association mappings information
+     *
+     * @param array $parentAssociationMappings
+     * @return void
+     */
+    function setParentAssociationMappings($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)
      *
@@ -246,4 +261,23 @@ interface FieldDescriptionInterface
      * @return string
      */
     function getLabel();
+
+    /*
+     * @return boolean
+     */
+    function isSortable();
+
+    /**
+     * return the field mapping definition used when sorting
+     *
+     * @return array the field mapping definition
+     */
+    function getSortFieldMapping();
+
+    /**
+     * return the parent association mapping definitions used when sorting
+     *
+     * @return array the parent association mapping definitions
+     */
+    function getSortParentAssociationMapping();
 }

+ 11 - 2
Datagrid/Datagrid.php

@@ -110,8 +110,17 @@ class Datagrid implements DatagridInterface
             $filter->apply($this->query, $data[$filter->getFormName()]);
         }
 
-        $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();
 

+ 1 - 1
Resources/views/CRUD/base_list.html.twig

@@ -40,7 +40,7 @@ file that was distributed with this source code.
                                     {% set sortable = false %}
                                     {% if field_description.options.sortable is defined and field_description.options.sortable%}
                                         {% set sortable             = true %}
-                                        {% set current              = admin.datagrid.values._sort_by == field_description.options.sortable %}
+                                        {% set current              = admin.datagrid.values._sort_by == field_description.name %}
                                         {% set sort_parameters      = admin.modelmanager.sortparameters(field_description, admin.datagrid) %}
                                         {% set sort_active_class    = current ? 'sonata-ba-list-field-order-active' : '' %}
                                         {% set sort_by              = current ? admin.datagrid.values._sort_order : field_description.options._sort_order %}