Quellcode durchsuchen

Merge pull request #111 from andrewtch/2.0

timestamp support
Thomas vor 13 Jahren
Ursprung
Commit
233fa4e03a
2 geänderte Dateien mit 36 neuen und 1 gelöschten Zeilen
  1. 13 1
      Filter/AbstractDateFilter.php
  2. 23 0
      Resources/doc/reference/filter_field_definition.rst

+ 13 - 1
Filter/AbstractDateFilter.php

@@ -43,6 +43,12 @@ abstract class AbstractDateFilter extends Filter
                 return;
             }
 
+            //transform types
+            if ($this->getOption('input_type') == 'timestamp') {
+                $data['value']['start'] = $data['value']['start'] instanceof \DateTime ? $data['value']['start']->getTimestamp() : 0;
+                $data['value']['end'] = $data['value']['end'] instanceof \DateTime ? $data['value']['end']->getTimestamp() : 0;
+            }
+
             //default type for range filter
             $data['type'] = !isset($data['type']) || !is_numeric($data['type']) ?  DateRangeType::TYPE_BETWEEN : $data['type'];
 
@@ -70,6 +76,11 @@ abstract class AbstractDateFilter extends Filter
             //just find an operator and apply query
             $operator = $this->getOperator($data['type']);
 
+            //transform types
+            if ($this->getOption('input_type') == 'timestamp') {
+                $data['value'] = $data['value'] instanceof \DateTime ? $data['value']->getTimestamp() : 0;
+            }
+
             //null / not null only check for col
             if (in_array($operator, array('NULL', 'NOT NULL'))) {
                 $this->applyWhere($queryBuilder, sprintf('%s.%s IS %s ', $alias, $field, $operator));
@@ -132,7 +143,8 @@ abstract class AbstractDateFilter extends Filter
         return array($name, array(
             'field_type'    => $this->getFieldType(),
             'field_options' => $this->getFieldOptions(),
-            'label'         => $this->getLabel()
+            'label'         => $this->getLabel(),
+            'input_type'    => 'datetime'
         ));
     }
 }

+ 23 - 0
Resources/doc/reference/filter_field_definition.rst

@@ -64,6 +64,29 @@ Example
         }
     }
 
+Timestamps
+----------
+
+``doctrine_orm_date``, ``doctrine_orm_date_range``, ``doctrine_orm_datetime`` and ``doctrine_orm_datetime_range`` support
+filtering of timestamp fields by specifying ``'input_type' => 'timestamp'`` option:
+
+.. code-block:: php
+
+    <?php
+    namespace Sonata\NewsBundle\Admin;
+
+    use Sonata\AdminBundle\Admin\Admin;
+    use Sonata\AdminBundle\Datagrid\DatagridMapper;
+
+    class PostAdmin extends Admin
+    {
+        protected function configureDatagridFilters(DatagridMapper $datagrid)
+        {
+            $datagrid
+                ->add('timestamp', 'doctrine_orm_datetime_range', array('input_type' => 'timestamp'));
+        }
+    }
+
 
 Advanced usage
 --------------