Sonata admin datagrid custom OR filtering

891 views
Skip to first unread message

Fabio_Zeta

unread,
Nov 23, 2015, 6:11:45 AM11/23/15
to sonata-devs

I'm using Symfony 2 with Sonata Admin Bundle.

As far as i can see, Sonata Admin has only AND filtering on list action.

Use this as example:

My entity : Prodotto

/**

 

* Prodotto

*

* @ORM\Table()

* @ORM\Entity

* @UniqueEntity("prodotto")

*/


class Prodotto extends DefaultEntity

{

/**

 * @var integer

 *

 * @ORM\Column(name="id", type="integer")

 * @ORM\Id

 * @ORM\GeneratedValue(strategy="AUTO")

 */


private $id;


 

//getters - setters Zone


 

/**

 * @param int $id

 */


public function setId($id)

{

    $this
->id = $id;

}


 

/**

 * @return int

 */


public function getId()

{

   
return $this->id;

}


 

/**

 * @var string

 *

 * @ORM\Column(name="prodotto", type="string", length=255)

 */


private $prodotto;


 


 

/**

 * @var \mybundle\Entity\Tipologia

 * @ORM\ManyToOne(targetEntity="Tipologia")

 */


private $tipologia;




/**

 * @var \mybundle\Entity\Brand

 * @ORM\JoinColumn(name="brand_id", referencedColumnName="id")

 * @ORM\ManyToOne(targetEntity="Brand")

 *

 */


private $brand;


/**

 * @var \mybundle\Entity\Layout

 * @ORM\ManyToOne(targetEntity="Layout")

 * @ORM\JoinColumn(name="layout_id", referencedColumnName="id")

 */


private $layout;


 
/**

 * @var \mybundle\Entity\Carta

 * @ORM\ManyToOne(targetEntity="Carta")

 * @ORM\JoinColumn(name="carta_id", referencedColumnName="id")

 */


private $carta;

 

//[... many other fields omitted...]

//[... Getters and setters omitted (default get/set, no custom code) ...]


My admin class: ProdottoAdmin (please note that i copied only the configuration useful for this question, the class contains all the required configurations for all the actions)


/**

 * @param DatagridMapper $datagridMapper

 */


protected function configureDatagridFilters(DatagridMapper $datagridMapper)

{

    $datagridMapper

       
->add('id')

       
->add('prodotto')

       
->add('tipologia')

       
->add('brand')

       
->add('layout')

       
->add('misura')

   
;

}
 

/**

 * @param ListMapper $listMapper

 */


protected function configureListFields(ListMapper $listMapper)

{

    $listMapper

       
->add('dataModifica','datetime',array('pattern' => 'dd/MM/yyyy','label'=>'Data'))

       
->add('tipologia')

       
->add('brand')

       
->add('layout')

       
->add('misura')

       
->add('Nome Prodotto', 'string', array('template' => 'mybundle:Admin:List/link_column_list_prodotto.html.twig'))


 

       
->add('_action', 'actions', array(

           
'actions' => array(

               
'show' => array(),

               
'edit' => array(),

               
'delete' => array(),

           
)

       
))

   
;

}


My Service Configuration (in services.yml):

services:

  mybundle.admin.prodotto:

      class: mybundle\Admin\ProdottoAdmin

      arguments: [~, mybundle\Entity\Prodotto, mybundle:ProdottoAdmin]

      tags:

          - {name: sonata.admin, manager_type: orm, group: Prodotti, label: Prodotti}


With this configuration, i actually got a fully functional data grid filter, as you can see from the picture(image added for better understanding of the problem):




But, the default Sonata Admin filtering expose only an AND filter, i can add ONE time all the property of the entity and make an AND filtering with them. But, now i need to extend that functionality. I need to add an OR filter, app-wide. I want that the user can filter like "give me all the product that are related with Brand1 OR Brand2 AND are of Tipologia1."

I know i can make precise custom query, like the example above, to obtain a single result, but:

  1. Is will not be app-wide, i have many entities and i can't write all the custom query needed
  2. Is verbose, i will have to write much of the same code in all the data grid filters
  3. Is not wise, because if tomorrow i change an entity, the data grid filter is coupled with the entity and i need to remember to add/modify the query.

So, finally, my question is:

There is a "correct" (or at least, a raccomended) way or pattern / maybe a configurable bundle to implement that OR filtering?

Reply all
Reply to author
Forward
0 new messages