Search code examples
phpsymfonysonata-admin

How can I filter several fields in one filter?


Is possible to create search filter to search from not one field, but from CONCAT(name, description) ?

protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
    $datagridMapper->add('search');
}

Solution

  • I found solution with doctrine_orm_callback type

    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
    {
    
    
        $datagridMapper
            ->add( 'text', 'doctrine_orm_callback', array(
                'callback' => array($this, 'getSearchFilter'),
                'field_type' => 'text'
            ) );
    }
    
    public function getSearchFilter($queryBuilder, $alias, $field, $value) {
        if (!$value['value']) {
            return;
        }
    
        $exp = new \Doctrine\ORM\Query\Expr();
        $queryBuilder->andWhere($exp->like($exp->concat($alias.'.name', $alias.'.description'), $exp->literal('%' . $value['value'] . '%')));
    
        return true;
    }