简体   繁体   English

奏鸣曲日期范围过滤器

[英]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 在第一个输入上,将时间设置为0:00
  • on the second input, you set a time to 23:59 在第二个输入上,您将时间设置为23:59

One other way is to use the doctrine_orm_datetime 另一种方法是使用doctrine_orm_datetime

Why 为什么

When you look at the parent abstract class Sonata\\DoctrineORMAdminBundle\\Filter\\AbstractDateFilter you can read in the filter method : 当您查看父抽象类Sonata\\DoctrineORMAdminBundle\\Filter\\AbstractDateFilter您可以在filter方法中读取:

 $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 . 这就像你在day/month/year:00:00day/month/year:00:00之间寻找的东西。

Not between day/month/year:00:00 and day/month/year:23:59 不在day/month/year:00:00day/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: 检查实体中的created_at属性并查看其名称,如果它已创建,则createdat和create_at是DB中的名称,因此您必须将其更改为:

->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 因为奏鸣曲管理员可以通过其DB名称显示属性,但它无法应用过滤器:)希望它有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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