我正在使用SonataAdmin来管理mysql表。
我有表参数
例如
name |heppening
stage1|2014-02-03 12:51:00
stage2|2014-03-12 18:24:00
stage3|2014-03-29 13:12:00
我想要获取一定时间之间的行。
timeA < happening and happening < timeB
但如果我像myAdmin这样写
class myAdmin扩展Admin {
public function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper
->add('happening');
}
它只会使精确定时的选择框。
如何在两段时间内指示两次搜索?
答案 0 :(得分:4)
你可以尝试使用doctrine_orm_callback这样的东西
protected function configureDatagridFilters(DatagridMapper $filter)
{
$filter->add('startDate', 'doctrine_orm_callback', [
'callback' => function($queryBuilder, $alias, $field, $value) {
if (!($value && isset($value['value']))) {
return;
}
$value = $value['value'];
$queryBuilder
->andWhere(sprintf('%s.startDate >= :start',$alias))
->setParameter('start', $value)
;
return true;
},
'attr' => array('class' => 'datepicker'),
], "date", array('widget' => 'single_text', 'attr' => array('class' => 'datepicker')))
->add('endDate', 'doctrine_orm_callback', [
'callback' => function($queryBuilder, $alias, $field, $value) {
if (!($value && isset($value['value']))) {
return;
}
//Handle your endDate with adding some hours to manage startDate = endDate
$value = $value['value']->modify("+24 hours");
$queryBuilder
->andWhere(sprintf('%s.endDate <= :end',$alias))
->setParameter('end', $value)
;
return true;
},
], "date", array('widget' => 'single_text', 'attr' => array('class' => 'datepicker')))
}