Skip to content

Instantly share code, notes, and snippets.

@worenga
Forked from dominikzogg/gist:2135954
Created March 23, 2012 23:43
Show Gist options
  • Save worenga/2176430 to your computer and use it in GitHub Desktop.
Save worenga/2176430 to your computer and use it in GitHub Desktop.
sonata admin daterange
->add('orderdatefrom', 'doctrine_orm_callback',
array
(
'callback' => function(\Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery $queryBuilder, $alias, $field, $options)
{
if(!isset($options['value']) || !$options['value'])
{
return;
}
$value = $options['value'];
// set defaults
if(empty($value['year']))
{
return;
}
if(empty($value['month']))
{
$value['month'] = 1;
}
if(empty($value['day']))
{
$value['day'] = 1;
}
// new datetime
$datetime = new \DateTime();
// update date
$datetime->setDate($value['year'], $value['month'], $value['day']);
$queryBuilder->andWhere(sprintf('%s.orderdate', $alias) . ' >= :orderdatefrom');
$queryBuilder->setParameter(":orderdatefrom", $datetime);
},
'field_type' => 'date',
'label' => 'statistic.orderdatefrom'
)
)
->add('orderdateto', 'doctrine_orm_callback',
array
(
'callback' => function(\Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery $queryBuilder, $alias, $field, $options)
{
if(!isset($options['value']) || !$options['value'])
{
return;
}
$value = $options['value'];
// set defaults
if(empty($value['year']))
{
return;
}
if(empty($value['month']))
{
$value['month'] = 1;
}
if(empty($value['day']))
{
$value['day'] = 1;
}
// new datetime
$datetime = new \DateTime();
// update date
$datetime->setDate($value['year'], $value['month'], $value['day']);
$queryBuilder->andWhere(sprintf('%s.orderdate', $alias) . ' <= :orderdateto');
$queryBuilder->setParameter(":orderdateto", $datetime);
},
'field_type' => 'date',
'label' => 'statistic.orderdateto'
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment