Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set format for datetime and date in sonata admin filters symfony2

How to set the datetime or date filters in sonata admin datagridfilters?

I want to do the following for the sonata admin filters, which works for edit form

->add('createdAt', 'datetime', array('label' => 'Created at', 'disabled' => true, 
                  'input' => 'datetime',
                  'date_widget' => 'choice',
                  'time_widget' => 'choice',
                  'date_format' => 'MMM d, y',))

->add('deadline', 'date', array('label' => 'Deadline', 'disabled' => true, 
                  'input' => 'datetime',
                  'widget' => 'choice',
                  'format' => 'MMM d, y'))

but it does not work (or the options are ignored) when used in filters using doctrine_orm_date and doctrine_orm_datetime

->add('createdAt', 'doctrine_orm_datetime', array('label' => 'Created At',
                'input' => 'datetime',
                  'date_widget' => 'choice',
                  'time_widget' => 'choice',
                  'date_format' => 'MMM d, y'))

->add('deadline', 'doctrine_orm_date', array('label' => 'Deadline',
                'input' => 'datetime',
                  'widget' => 'choice',
                  'format' => 'MMM d, y'))

The reason that I am forced to do this is because, on my server (centos 5.2, php 5.3.20) the month field is being rendered as timestamp but on my dev machine it is rendered perfectly - there are a few questions regarding this issue but no real fix. These 2 links describe my problem main problem - e.g. symfony2 - date choice input renders timestamp instead of month name, http://iqwen.net/question/155068

so I would like to know 3 things

  1. how to set the format option for datetime / date fields in the sonata admin filters
  2. Is there a way to fix the issue where month appears as timestamp on linux env
  3. How can i set a global format option for date / datetime field in symfony2 / sonata admin so that I do not have to specify the format next to each field

Any help regarding wiill be greatly appreciated.

like image 495
dagger Avatar asked Jan 21 '13 13:01

dagger


1 Answers

You can do it in this way:

->add('createdAt', 'doctrine_orm_callback', 
              array(
                'label' => 'Created At',
                'callback' => function($queryBuilder, $alias, $field, $value) {
                                if (!$value['value']) {
                                    return;
                                }
                                $time = strtotime($value['value']);
                                $inputValue = date('Y-m-d', $time);
                                $queryBuilder->andWhere("DATE($alias.createdAt) <= :CreatedAt");
                                $queryBuilder->setParameter('CreatedAt', $inputValue);
                                return true;
                              },
                'field_type' => 'text'
              ), null, array('attr' => array('class' => 'datepicker')))

The DATE function is a cast that you define with DQL.

like image 147
Francis Gonzales Avatar answered Sep 29 '22 22:09

Francis Gonzales