繁体   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;
    // ...
  }

此代码生成具有两个值的过滤器:是/否。 “yes”选项将在字段中返回值为“true”的行,过滤器的“false”选项将在db字段中返回值为“false”的行,但如何在“no”过滤器选项中包含值为“null”的行?

如果您在 add() 选项中设置它们,您可以为过滤器添加自定义选项。

对于 4.3 之前的 symfony 版本和 sonata-admin-bundle 3,您可以这样做:

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

对于最新版本(我已经在本地测试并为我工作)

->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