Sonata / symfony - setting parent / child structure

I have been asking for this for a while. I can’t believe that there is not a single developer who would not know the answer, and I was a little desperate.

In Sonata, I cannot get the url / pattern / parent / ID / child / list structure to work. Went very, pretty poor, 4.6. CREATE CHILD ADMINS in sonata docs, found only a few examples on the Internet, and I can't get it to work.

Can someone explain step by step how to set up such a structure?

+5
source share
1 answer

I use the example provided by Sonata in my explanation, the main Post / Comment relation.

You should have a parent / child (oneTomany / manyToOne) link between your Post (parent) and Comment (child) objects.

You must add addChild arguments to the service declaration that target the child admin service:

services.yml

sonata.news.admin.comment: class: Sonata\NewsBundle\Admin\CommentAdmin arguments: [~, Sonata\NewsBundle\Model\Comment, SonataNewsBundle:CommentAdmin] tags: - {name: sonata.admin, manager_type: orm, group: "Content"} sonata.news.admin.post: class: Sonata\NewsBundle\Admin\PostAdmin arguments: [~, Sonata\NewsBundle\Model\Post, SonataNewsBundle:PostAdmin] tags: - {name: sonata.admin, manager_type: orm, group: "Content"} calls: - [addChild, ['@sonata.news.admin.comment']] 

In your CommentAdmin class, you need to add the AssociationMapping property to filter this child by the parent.

CommentAdmin

 class CommentAdmin extends Admin { protected $parentAssociationMapping = 'post'; ... } 

Then you will have a new route available: / parent / ID / child / list, you can use the console to determine the identifier of your new route (php app / console router: debug). If you need an easy way to access this in the administrator, I suggest adding a button to the direct list of administrators to access her child comments:

Create a template that adds a button to access comments for children:

post_comments.html.twig

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

Then add the button action in the parent Admin in this case, PostAdmin:

Postadmin

 class PostAdmin extends Admin { protected function configureListFields(ListMapper $listMapper) { $listMapper->add('_action', 'actions', array( 'actions' => array( 'show' => array(), 'edit' => array(), 'comments' => array('template' => 'PATH_TWIG') ) )) } } 

Hope you learned a little more about how to set up parent / child admins.

+14
source

Source: https://habr.com/ru/post/1206203/


All Articles