Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7938259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:45:27+00:00 2026-06-03T22:45:27+00:00

I’m using doctrine inheritance mapping to enable various objects to be linked to a

  • 0

I’m using doctrine inheritance mapping to enable various objects to be linked to a comment entity. This is achieved through various concrete “Threads”, which have a one-to-many relationship with comments. So taking a ‘Story’ element as an example, there would be a related ‘StoryThread’ entity, which can have many comments.

That is all working fine, but I’m having troubles trying to define a CommentAdmin class for the SonataAdminBundle that can be used as a child of the parent entities. For example, I’d want to be able to use routes such as:

/admin/bundle/story/story/1/comment/list
/admin/bundle/media/gallery/1/comment/list

Does anyone have any pointers about how I can go about achieving this? I’d love to post some code extracts but I haven’t managed to find any related documentation so don’t really know the best place to start.

I’ve been trying to use the SonataNewsBundle as a reference because they’ve implemented a similar parent/child admin relationship between posts and comments, but it appears as though this relies on the ‘comment’ (child) admin class to be hardcoded to know that it belongs to posts, and it also seems as though it needs to have a direct many-to-one relationship with the parent object, whereas mine is through a separate “Thread” entity.

I hope this makes sense! Thanks for any help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T22:45:28+00:00Added an answer on June 3, 2026 at 10:45 pm

    Ok I managed to get this working eventually. I wasn’t able to benefit from using the $parentAssociationMapping property of the CommentAdmin class, as the parent entity of a comment is a concrete instance of the Thread entity whereas the parent ‘admin’ class in this case is a Story (which is linked via the StoryThread). Plus this will need to remain dynamic for when I implement comments on other types of entity.

    First of all, I had to configure my StoryAdmin (and any other admin classes that will have CommentAdmin as a child) to call the addChild method:

    acme_story.admin.story:
          class: Acme\Bundle\StoryBundle\Admin\StoryAdmin
          tags:
            - { name: sonata.admin, manager_type: orm, group: content, label: Stories }
          arguments: [null, Acme\Bundle\StoryBundle\Entity\Story, AcmeStoryBundle:StoryAdmin]
          calls:
              - [ addChild, [ @acme_comment.admin.comment ] ]
              - [ setSecurityContext, [ @security.context ] ]
    

    This allowed me to link to the child admin section from the story admin, in my case from a side menu, like so:

    protected function configureSideMenu(MenuItemInterface $menu, $action, Admin $childAdmin = null)
    {
        // ...other side menu stuff
    
        $menu->addChild(
            'comments',
            array('uri' => $admin->generateUrl('acme_comment.admin.comment.list', array('id' => $id)))
        );
    }
    

    Then, in my CommentAdmin class, I had to access the relevant Thread entity based on the parent object (e.g a StoryThread in this case) and set this as a filter parameter. This is essentially what is done automatically using the $parentAssociationMapping property if the parent entity is the same as the parent admin, which it most likely will be if you aren’t using inheritance mapping. Here is the required code from CommentAdmin:

    /**
     * @param \Sonata\AdminBundle\Datagrid\DatagridMapper $filter
     */
    protected function configureDatagridFilters(DatagridMapper $filter)
    {
        $filter->add('thread');
    }
    
    
    /**
     * @return array
     */
    public function getFilterParameters()
    {
        $parameters = parent::getFilterParameters();
    
        return array_merge($parameters, array(
            'thread' => array('value' => $this->getThread()->getId())
        ));
    }
    
    public function getNewInstance()
    {
        $comment = parent::getNewInstance();
    
        $comment->setThread($this->getThread());
        $comment->setAuthor($this->securityContext->getToken()->getUser());
    
        return $comment;
    }
    
    /**
     * @return CommentableInterface
     */
    protected function getParentObject()
    {
        return $this->getParent()->getObject($this->getParent()->getRequest()->get('id'));
    }
    
    /**
     * @return object Thread
     */
    protected function getThread()
    {
        /** @var $threadRepository ThreadRepository */
        $threadRepository = $this->em->getRepository($this->getParentObject()->getThreadEntityName());
    
        return $threadRepository->findOneBy(array(
            $threadRepository->getObjectColumn() => $this->getParentObject()->getId()
        ));
    }
    
    /**
     * @param \Doctrine\ORM\EntityManager $em
     */
    public function setEntityManager($em)
    {
        $this->em = $em;
    }
    
    /**
     * @param \Symfony\Component\Security\Core\SecurityContextInterface $securityContext
     */
    public function setSecurityContext(SecurityContextInterface $securityContext)
    {
        $this->securityContext = $securityContext;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am reading a book about Javascript and jQuery and using one of the
this is what i have right now Drawing an RSS feed into the php,
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.