Single and Multiple Ordering To-Many Associations in Doctrine2

Associations in Doctrine2 can be ordered by default without using an extra query:


This also works for multiple ordering:



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:



FOSUserBundle login error ‘Could not convert database value “” to Doctrine Type array’

Refactoring an existing project and migrating to Symfony2 I had to import 4000 users into the FOSUserBundle table fos_user.

I wrote a little import script that unfortunately updated some columns with an empty string. These columns were used by Doctrine for storing arrays:

  • roles (DC2Type:array)
  • facebook_data (DC2Type:json)
  • twitter_data (DC2Type:json)
  • gplus_data (DC2Type:json)

After trying to login I got the following error:

Could not convert database value “” to Doctrine Type array

The solution was to replace the empty string with the expected array e.g.:

If you want to add Roles you can use the FOSUserBundle command line tools:

FOSUserBundle Command Line Tools:



How to use orderBy param on find_method findAll with Doctrine ObjectSelect

The docs offer an example for an extended version of the DoctrineModule\Form\Element\ObjectSelect:

This find_method parameters allow you to order and sort the result of your repository method.


Unfortunately this will not work when using the findAll method. But why?

Looking at the source code of the Doctrine ORM you can see that unlike the findBy method findAll does not allow passing arguments for filtering, sorting nor limiting:


But there is a workaround:

Use the findBy() method instead and simply pass an empty array for the criteria parameter.


That’s it!



How to render XML response in Zend Framework 2

Zend Framework 2 offers different Rendering and Response Strategies.

Unfortunately there is no out-of-the-box Strategy for XML.

Solely RSS Feeds are supported:

Well, there is a Module by Alessandro Pietrobelli:

If you want to render raw XML you can use these simple lines in your controller:


If you need to create complex XML files you can use the PHP XMLWriter which is included in ZF2:



Doctrine Debug dump usage instead of var_dump

Lazy load proxies always contain an instance of Doctrine’s EntityManager and all its dependencies. Therefore a var_dump() will possibly dump a very large recursive structure which is impossible to render and read.
You have to use Doctrine\Common\Util\Debug::dump() to restrict the dumping to a human readable level.
Additionally you should be aware that dumping the EntityManager to a Browser may take several minutes, and the Debug::dump() method just ignores any occurrences of it in Proxy instances.

See tutorial:


Checking ACL in Zend Navigation when using custom view script

The ZF2 Zend Navigation View Helpers manual shows how to configure your navigation adding ACL.

It also shows how to manually render your navigation inside your view scripts or layout:

Or simply:

So far so good – all of your ACL should work fine and only allowed pages are rendered.

This behaviour changes when you render your pages partially. The manual shows this example code:

This way also not allowed pages will be rendered. In order to get ACL checked you need to use the accept() method:




Check Zend Navigation page permissions with ZfcRbac

Define your navigation adding pages and permissions:


Configure your guards and role_providers for ZfcRbac:


Create a Listener:


Create a Factory for the RbacListener:


Add your RbacListenerFactory to your ServiceManager;


And finally attach an event to the isAllowed method of the Zend Navigation View Helper:


Now, if you render your navigation inside your layout only the pages the user role is permitted to see should be rendered.