Add fullText search (WHERE LIKE OR) with SonataDoctrineORMAdminBundle to datagridFilters in SonataAdminBundle

Sometimes you want to add a full text search filter (e.g. for “username” and “email” etc.) to the datagrid without having to add a single filter for every field.



Create custom template list field for title and name with SonataUserBundle User Entity GENDER constants in SonataAdminBundle

The SonataUserBundle UserInterface uses the following GENDER constants:

We will use the gender to determine the title of a User and prefix it to the full name. To set a custom template in my UserAdmin class we configure the field:

The following custom template uses older Twig Syntax to retrieve the constants:

The following version is recommended and uses Twig Syntax (e.g. “ternary operator”) since 1.12.0:


Populate resp. set default values on form resp. object or instance in SonataAdminBundle

To set default values on a new instance you can override the getNewInstance() method in your Admin class:

This is a pretty basic example for setting columns that expect a string, bool etc.

If you need to set an related Object you can not simply pass an identifier (e.g. company_id). You will have to fetch the instance:

Fetching a complete Object actually causes some overhead. Instead it is recommended to only fetch the reference:

 See Doctrine documentation for details.

If you need to get the identifier from the Request you can use the getRequest() method:


The featured parts of the User Entity: