How can I create a custom DataGrid filter in SonataAdmin

后端 未结 2 450
广开言路
广开言路 2021-02-04 18:53

I have an entity Transaction with numerous status codes. I want the user to be able to see these status codes as strings in SonataAdmin. The user should also be able to filter

2条回答
  •  北海茫月
    2021-02-04 19:23

    Something like that

    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
    {
        $datagridMapper
            ->add('codes', 'doctrine_orm_callback', array(
                'label'         => 'Код',
                'callback'      => array($this, 'getCodesFilter'),
                'field_type'    => 'genemu_jquerychosen',
                'field_options' => array(
                    'class'     => 'Mtools\ClientBundle\Entity\Client',
                    'widget'    => 'entity',
                    'multiple'  => false,
                )
            );
    }
    
    public function getCodesFilter($queryBuilder, $alias, $field, $value)
    {
        if (!$value) {
            return;
        }
        $queryBuilder->leftJoin(sprintf('%s.codes', $alias), 'c');
        $queryBuilder->andWhere('c.code = :code');
        $queryBuilder->setParameter('code', $value['value']);
    }
    

提交回复
热议问题