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

admin.comment:
    class: Sonata\YourBundle\Admin\CommentAdmin
    arguments: [~, Sonata\YourBundle\Entity\Comment,~]
    tags:
        - {name: sonata.admin, manager_type: orm, group: "Content"}
admin.post:
    class: Sonata\YourBundle\Admin\PostAdmin
    arguments: [~, Sonata\YourBundle\Entity\Post, ~]
    tags:
        - {name: sonata.admin, manager_type: orm, group: "Content"}
    calls:
        - [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': object.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 – http://symfony.com/blog/paving-the-way-for-symfony-3-with-the-deprecation-detector-tool

Complete list of changes