Using PHP YAML Extension and Unicode on Entity column with Doctrine Events

I have a Rails legacy database that uses a YAML formatted array in a column. My new App is based on Symfony2 and Doctrine2.

To make this column useable in Sonata Admin I had to (re-)convert between PHP Array and YAML.


This worked fine using Doctrine Events:


Note the YAML_UTF8_ENCODING constant when emitting. By default yaml_emit uses the YAML_ANY_ENCODING constant.

All constants can be found here:

How to check if Subject is New resp. action is create or edit in Sonata Admin

In your Admin Class:

In your Twig Template:




Override list view twig template in SonataAdminBundle

There are two ways to override the list view template.

1. Admin Class

2. Service Setter Injection

As described in the docs.


Then take the original twig template and change the desired block e.g. list_header:


That’s it!




Translating dynamic vars concatening object property in twig template using SonataAdminBundle

My Contract Entity has a history of states and the current state is displayed in the listView. The state is stored as a simple string e.g. “pending” in the database.


But the translation string are stored with a prefix e.g. “contracts.states.pending”:


To make the translation work inside the twig template you can concat the prefix and the property from the database like this:



Override CRUDController to add custom form elements to list template for batch action usage in SonataAdminBundle

In my use case I needed a batch action with an additional date field to be set on my Entity. I came up with the following solution.

I will not add the Entities since this post should focus on the configuration of the list template and batch action.


First of all define the custom batch action and the path to your custom list template in your Admin class:


Next enable and configure a custom CRUDController:


Then create your custom CRUDController:


Copy the original listAction from CRUDController.php and add the custom form date element:

We can use the sonata_type_date_picker which comes built-in with the SonataAdminBundle.

Next add the date picker to your template:

This should render some HTML and Javascript for the element looking like this:


In our Admin class we set ask_confirmation to true. This way we can make use of the relevancy check function to validate our new form element.

Regarding the naming conventions add a batchActionTestIsRelevant method to your CRUDController:


Finally we add the batchActionTest method:

And we’re done!



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: