简体   繁体   中英

Sonata date range filter

I have a sonata date range filter

protected function configureDatagridFilters(DatagridMapper $filter)
    {
        $now = new \DateTime();
        $filter
            ->add('created_at', 'doctrine_orm_date_range',  array(
                    'label' => 'created_at_long',
                    'input_type' => 'text',
                    'field_options' => array(
                        'widget' => 'single_text'

                    )
                )
            );
    }

If I enter the same date, elements with this date are not returned in my list. Is there a way to tell sonata do not use strict comparaison and use "less than or equal" and "greater than or equal" operators ?

I think adding a format tag will relax the constraints. Works for (is inclusive) me anyway with the following:

'format' => 'dd/MM/yyyy'

From this currently live/working filter:

->add('someField', 'doctrine_orm_date_range', [], null, ['format' => 'dd/MM/yyyy', 'widget' => 'single_text'])

I think you should create your own filter extending this one.

  • On the first input, you set a time to 0:00
  • on the second input, you set a time to 23:59

One other way is to use the doctrine_orm_datetime

Why

When you look at the parent abstract class Sonata\\DoctrineORMAdminBundle\\Filter\\AbstractDateFilter you can read in the filter method :

 $data['value']['start'] = $data['value']['start'] instanceof \DateTime ? $data['value']['start']->getTimestamp() : 0;
 $data['value']['end'] = $data['value']['end'] instanceof \DateTime ? $data['value']['end']->getTimestamp() : 0;

It's like you where looking for something between day/month/year:00:00 and day/month/year:00:00 .

Not between day/month/year:00:00 and day/month/year:23:59

check the created_at attribute in the entity and see its name, if it is createdAt and created_at is its name in the DB so you have to change it to:

->add('createdAt', 'doctrine_orm_date_range',  array(
                    'label' => 'created_at_long',
                    'input_type' => 'text',
                    'field_options' => array(
                        'widget' => 'single_text'

                    )
                )
            );

because the sonata admin could display the attribute by its DB name but it couldn't apply the filter with it :) hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM