如何在sonata admin datagridfilters中设置日期时间或日期过滤器?
我想为奏鸣曲管理过滤器执行以下操作,该过滤器适用于编辑表单
->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'))
但在使用doctrine_orm_date和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'))
我被迫这样做的原因是,在我的服务器(centos 5.2,php 5.3.20)上,月份字段被呈现为时间戳,但在我的开发机器上它被完美呈现 - 有几个问题关于这个问题但没有真正解决。这两个链接描述了我的问题主要问题 - 例如symfony2 - date choice input renders timestamp instead of month name, http://iqwen.net/question/155068
所以我想知道3件事
非常感谢任何关于wiill的帮助。
答案 0 :(得分:1)
你可以这样做:
->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')))
DATE函数是您使用DQL定义的强制转换。