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
Any help regarding wiill be greatly appreciated.
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.