Sonata Admin Bundle: can I add an admin child that can have different parents?

I use doctrine inheritance matching to include various objects in a comment object. This is achieved through various specific "threads" that have a one-to-many relationship with comments. Therefore, taking the History element as an example, there will be a related StoryThread object that can have many comments.

Everything works fine, but I am having problems defining the CommentAdmin class for SonataAdminBundle, which can be used as a child of the parent objects. For example, I would like 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 directions on how I can do this? I would like to post some code snippets, but I couldn’t find the relevant documentation, so I don’t know what is the best way to start.

I tried to use SonataNewsBundle as a reference because they implemented a similar relationship between parents and child administrators between posts and comments, but it looks like it belongs to the admin (comment) (child) admin class to be hard-coded to know that it belongs to messages, and it also seems that it should have a direct many-to-one relationship with the parent object, while mine - through a separate Thread object.

Hope this makes sense! Thanks for any help.

+3
2

, . $parentAssociationMapping CommentAdmin, Thread, "admin" ( StoryThread). , , .

, StoryAdmin ( , CommentAdmin ), addChild:

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 ] ]

, , :

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)))
    );
}

CommentAdmin Thread (, StoryThread ) . $parentAssociationMapping, , , , , . 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;
}
+16

:

 public function getParentAssociationMapping()
 {
    // we grab our entity manager
    $em = $this->modelManager->getEntityManager('acme\Bundle\Entity\acme'); 

    // we get our parent object table name
    $className = $em->getClassMetadata(get_class($this->getParent()->getObject($this->getParent()->getRequest()->get('id'))))->getTableName();

    // we return our class name ( i lower it because my tables first characted uppercased )
    return strtolower( $className );
 }

, inversedBy $className,

0

All Articles