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!

 

Backlinks:

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:

 

Backlinks:

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.

 

Backlinks:

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:

 

Backlinks:

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:

https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/command_line_tools.md

 

Backlinks:

http://stackoverflow.com/questions/8818387/doctrine2-conversion-error

jqGrid TypeError: b.jgrid.formatter is undefined

When you get the following error message:

TypeError: b.jgrid.formatter is undefined

then you typically missed to import the required language file in the format grid.locale-XX.js  (e.g. grid.locale-en.js).

This file must be imported before the jqGrid source (jquery.jqGrid.min.js or jquery.jqGrid.src.js):

As provided by the wiki: http://www.trirand.com/jqgridwiki/doku.php?id=wiki%3afirst_grid#html_file

 

Backlinks:

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:

https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/EntityRepository.php#L157-182

 

But there is a workaround:

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

 

That’s it!

 

Backlinks:

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:

 

Backlinks:

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: http://docs.doctrine-project.org/en/2.1/tutorials/getting-started-xml-edition.html#a-first-prototype