简体   繁体   中英

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?

You can add custom choices for your filter if you set them in your add() options.

For symfony versions before 4.3 and sonata-admin-bundle 3 you can do it like this:

$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]])

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