Browse Source

add limit management, fix weird bug with doctrine and left join query

Thomas 14 years ago
parent
commit
24ae696760
4 changed files with 57 additions and 18 deletions
  1. 24 0
      Admin/Admin.php
  2. 1 1
      Filter/CallbackFilter.php
  3. 14 11
      Filter/Filter.php
  4. 18 6
      Tool/Datagrid.php

+ 24 - 0
Admin/Admin.php

@@ -26,6 +26,8 @@ abstract class Admin extends ContainerAware
 
     protected $filter_datagrid;
 
+    protected $max_per_page = 25;
+
     protected $base_route = '';
 
     protected $base_controller_name;
@@ -357,6 +359,8 @@ abstract class Admin extends ContainerAware
                 $this->getEntityManager()
             );
 
+            $this->filter_datagrid->setMaxPerPage($this->max_per_page);
+
             $this->configureFilterFields();
             
             $this->filter_datagrid->setFilterFields($this->filter_fields);
@@ -555,4 +559,24 @@ abstract class Admin extends ContainerAware
     {
         return $this->label;
     }
+
+    public function setFilterFields($filter_fields)
+    {
+        $this->filter_fields = $filter_fields;
+    }
+
+    public function getFilterFields()
+    {
+        return $this->filter_fields;
+    }
+
+    public function setMaxPerPage($max_per_page)
+    {
+        $this->max_per_page = $max_per_page;
+    }
+
+    public function getMaxPerPage()
+    {
+        return $this->max_per_page;
+    }
 }

+ 1 - 1
Filter/CallbackFilter.php

@@ -15,7 +15,7 @@ namespace Bundle\BaseApplicationBundle\Filter;
 class CallbackFilter extends Filter
 {
 
-    protected function association($query_builder)
+    protected function association($query_builder, $value)
     {
         return array($query_builder->getRootAlias(), false);
     }

+ 14 - 11
Filter/Filter.php

@@ -60,23 +60,26 @@ abstract class Filter extends Configurable
 
         $this->field->bind($value);
 
-        list($alias, $field) = $this->association($query_builder);
+        list($alias, $field) = $this->association($query_builder, $this->field->getData());
 
         $this->filter($query_builder, $alias, $field, $this->field->getData());
     }
 
-    protected function association($query_builder)
+    protected function association($query_builder, $value)
     {
-        if($this->description['type'] == \Doctrine\ORM\Mapping\ClassMetadataInfo::MANY_TO_MANY) {
-            $query_builder->leftJoin(
-                sprintf('%s.%s', $query_builder->getRootAlias(), $this->description['fieldName']),
-                $this->getName()
-            );
-
-            // todo : use the metadata information to find the correct column name
-            return array($this->getName(), 'id');
-        }
+        if($value) {
+
+            if($this->description['type'] == \Doctrine\ORM\Mapping\ClassMetadataInfo::MANY_TO_MANY) {
+                $query_builder->leftJoin(
+                    sprintf('%s.%s', $query_builder->getRootAlias(), $this->description['fieldName']),
+                    $this->getName()
+                );
 
+                // todo : use the metadata information to find the correct column name
+                return array($this->getName(), 'id');
+            }
+        }
+        
         return array($query_builder->getRootAlias(), $this->description['fieldName']);
     }
 

+ 18 - 6
Tool/Datagrid.php

@@ -44,25 +44,27 @@ class Datagrid
 
     protected $pager;
 
+    protected $max_per_page = 25;
+
     public function __construct($classname, $entity_manager, $values = array())
     {
-        $this->classname = $classname;
-        $this->entity_manager = $entity_manager;
-        $this->values = $values;
+        $this->classname        = $classname;
+        $this->entity_manager   = $entity_manager;
+        $this->values           = $values;
     }
 
     public function getClassMetaData()
     {
-        $em             = $this->getEntityManager();
 
-        return $em->getClassMetaData($this->getClassname());
+        return $this->getEntityManager()
+            ->getClassMetaData($this->getClassname());
     }
 
     public function getPager()
     {
 
         if(!$this->pager) {
-            $this->pager = new Pager($this->getClassname());
+            $this->pager = new Pager($this->getClassname(), $this->getMaxPerPage());
 
             $this->pager->setQueryBuilder($this->getQueryBuilder($this->values));
             $this->pager->setPage(isset($this->values['page']) ? $this->values['page'] : 1);
@@ -294,4 +296,14 @@ class Datagrid
     {
         return $this->values;
     }
+
+    public function setMaxPerPage($max_per_page)
+    {
+        $this->max_per_page = $max_per_page;
+    }
+
+    public function getMaxPerPage()
+    {
+        return $this->max_per_page;
+    }
 }