Sonata Admin Bundle nested list or has child list

Post (parent) to display comments (child) list.

Creating list of rows on sonata admin bundle is fairly easily with there documentation. But when you want to go bit advance on creating sub list of parent entity, then you would be scratching your head.  Luckily this post will help you overcome this very easily.

Assuming you already have PostAdmin and CommentAdmin class to display list individually, if you don’t then please create them first.

We will tell our child class, who is his father (parent). 

// .. YourBundle/Admin/CommentAdmin.php

class CommentAdmin extends Admin
    protected $parentAssociationMapping = 'post'; 

Now we can create services for parent and child, if you already have created the services, then please make sure you add call method on parent service. 

// .. service.yml

    class: Sonata\YourBundle\Admin\CommentAdmin
    arguments: [~, Sonata\YourBundle\Entity\Comment,~]
        - {name: sonata.admin, manager_type: orm, group: "Content"}
    class: Sonata\YourBundle\Admin\PostAdmin
    arguments: [~, Sonata\YourBundle\Entity\Post, ~]
        - {name: sonata.admin, manager_type: orm, group: "Content"}
        - [addChild, ['@admin.comment']]

That’s it!

We can now add links on post list page, but you can add link on any page as you like. 

Create template to add link

// ..YourBundle/Resources/views/CRUD/post_comments.html.twig

<a class="btn btn-sm btn-default" href="{{ path('OUR_NEW_CHILD_ROUTE_ID', {'id': }) }}">Comments</a>

Replace OUR_NEW_CHILD_ROUTE_ID with comment list route id, which you can find it with following command line.

$ php app/console debug:route

Now we are ready to add link on post list page for each post. 

// ..YourBundle/Admin/PostAdmin.php

class PostAdmin extends Admin
    protected function configureListFields(ListMapper $listMapper)
        $listMapper->add('_action', 'actions', array(
            'actions' => array(
                'show' => array(),
                'edit' => array(),
                'comments' => array('template' => 'YourBundle:CRUD:post_comments.html.twig')

That’s it you are done. Now all post are linked properly with its child comments as oneToMany relationship. 

SonataAdminBundle has so much more to offer, which can be hidden if you are new to it, but once you learn it. Then creating admin area would take you few days or less then week depending on the size of your application. 

Symfony 3 should I upgrade or stay with Symfony 2

Everyone is excited about Symfony 3, including myself :)

Symfony 3 or Symfony 2?

Symfony 2.8 is exactly same as Symfony 3. But wait, then what’s the difference?

Symfony 3 mostly have new directory structure for logs, cache and console. Also all the previous deprecated functions are removed.

Should I upgrade then, if its same?

No!!!, Even tho it is exactly same, but knowing all the deprecated functions are removed on Symfony 3, you should ask yourself.

Are you going to be using third party bundles?

and thous bundles are update-to-date with Symfony 3. Most likely you will get the answer no.

having third party bundles on Symfony 3 will break your application, If it was using any of the deprecated function.

Soon we can upgrade when everyone upgrades their bundles.

Updated – Wed 17 Feb

You can also check your vendor packages, if they are using any old deprecated functions, check following link for more details –

Complete list of changes

Symfony3 Auto Wiring – Auto Creating Dependency Services

Previously if you ever had dependency for your object, you had to make sure the names for dependency services are correctly passed down to your new service before you can start using it.

Since Symfony 2.8 which is official exact version of Symfony 3 (removing all the deprecated functions).

Now you can create services without worrying about dependency injection services with auto wiring feature. The key feature in auto wiring is, if one of your dependency injection service does not exist, then it will auto create it and inject it.

Let’s assume we have following object, which needs to be created as service.
namespace AppBundle\Service;

use AppBundle\Service\House;

class Room
    private $house;

    public function __construct(House $house)
        $this->house = $house;

Old way creating service
        class: AppBundle\Service\House

        class: AppBundle\Service\Room
            - @house
New way with auto wiring
        class: AppBundle\Service\Room
        autowire: true

The above example is very simple. Now imagine you have 5 or even 6 different dependency injections, you would have to waste time on getting each dependency service names and on top, if you later on decided to change the or remove dependency from your object, then you have to update services as well. With auto wiring you don’t have to worry about anything, it worries for you.

What about Performance?

It is exactly as same as before. Compiler builds and save everything in cache, just like before.

Change input name of the form fields in Symfony3

Remove form type name from the form

Sometimes we need to integrate third party application or form, which requires us to remove any array type form names. 


Normal form will create FormTypeName[fieldName]

<input type="text" value="" name="formTypeName[fieldName]">

Our final outcome should look like field name only without any form type name.

<input type="text" value="" name="fieldName">


Create the form using createNamed method.

$form = $this->get('form.factory')->createNamed(null, new MyFormType(), $dataObject, $formOptions);

When you pass first argument as null, it will remove form type name and make fields as field name only.

Solution 2

You can also change your formType and return blockPrefix null.

class MyFormType extends AbstractType

     * This will remove formTypeName from the form
     * @return null
    public function getBlockPrefix() {
        return null;

I recommend first solution.