简体   繁体   English

在 Sonata boolean 过滤器中包含 null 值

[英]Include null values in Sonata boolean filter

final class SomeAdmin extends AbstractAdmin
{      
  protected function configureDatagridFilters(DatagridMapper $datagridMapper)
  {       
      $datagridMapper->add('sending_error', null, [
            'label' => 'some label;',
      ]);
  }
}

  // ...
  class Entity
  {
    /**
     * @var bool|null
     *
     * @ORM\Column(type="boolean", nullable=true)
     */
    private $sending_error;
    // ...
  }

This code generates a filter with two values: yes / no.此代码生成具有两个值的过滤器:是/否。 "yes" option will return rows with value "true" in field, "false" option of the filter will return rows with value "false" in db field, but how to include rows with "null" value in "no" filter option? “yes”选项将在字段中返回值为“true”的行,过滤器的“false”选项将在db字段中返回值为“false”的行,但如何在“no”过滤器选项中包含值为“null”的行?

You can add custom choices for your filter if you set them in your add() options.如果您在 add() 选项中设置它们,您可以为过滤器添加自定义选项。

For symfony versions before 4.3 and sonata-admin-bundle 3 you can do it like this:对于 4.3 之前的 symfony 版本和 sonata-admin-bundle 3,您可以这样做:

$datagridMapper
   ->add('sending_error',
    'doctrine_orm_string',
    array(), 
   'choice',
    array('choices' => array('m' => 'Male', 'f' => 'Female')
    )
);

And for the newest versions (this i have test locally and works for me)对于最新版本(我已经在本地测试并为我工作)

->add('sending_error', null, ['label' => 'some label'], ChoiceType::class, [
                        'choices' => ['True' => True, 'False' =>False,'Empty'=>null]])

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

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