Skip to content

Instantly share code, notes, and snippets.

@dominikzogg
Created March 20, 2012 14:04
Show Gist options
  • Save dominikzogg/2135954 to your computer and use it in GitHub Desktop.
Save dominikzogg/2135954 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->format('Y-m-d'));
},
'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->format('Y-m-d'));
},
'field_type' => 'date',
'label' => 'statistic.orderdateto'
)
)
@worenga
Copy link

worenga commented Apr 2, 2012

theres a typo:

$queryBuilder->setParameter(":orderdateto", $datetime);
has to be
$queryBuilder->setParameter("orderdateto", $datetime);

same above, thank you for sharing though!

@dominikzogg
Copy link
Author

I don't see a typo, i replace the ':orderdateto' in the first with the ':orderdateto' in the second.

@worenga
Copy link

worenga commented Apr 2, 2012 via email

@dominikzogg
Copy link
Author

@dominikzogg
Copy link
Author

Changed: $queryBuilder->setParameter("orderdatefrom", $datetime->format('Y-m-d')); cause if i use datetime itself, i didn't get the items of the date from

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment