Sonata管理捆绑包:可以添加可以具有不同父级的子管理对象


Sonata Admin Bundle: possible to add a child admin object that can have different parents?

我正在使用学说继承映射来使各种对象链接到注释实体。这是通过各种具体的"线程"实现的,这些"线程"与注释具有一对多关系。因此,以"故事"元素为例,会有一个相关的"StoryThread"实体,它可以有很多注释。

这一切都工作正常,但是我在尝试为SonataAdminBundle定义可用作父实体的子级的CommentAdmin类时遇到了麻烦。例如,我希望能够使用以下路由:

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

有没有人对我如何实现这一目标有任何指示?我很想发布一些代码摘录,但我还没有找到任何相关的文档,所以真的不知道最好的起点。

我一直在尝试使用 SonataNewsBundle 作为参考,因为他们在帖子和评论之间实现了类似的父/子管理关系,但这似乎依赖于"评论"(子(管理员类进行硬编码知道它属于帖子,而且似乎它也需要与父对象建立直接的多对一关系, 而我的是通过一个单独的"线程"实体。

我希望这是有道理的!感谢您的任何帮助。

好的,

我最终设法让它工作。我无法从使用 CommentAdmin 类的 $parentAssociationMapping 属性中受益,因为注释的父实体是 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 类中,我必须基于父对象(例如本例中的 StoryThread(访问相关的 Thread 实体,并将其设置为过滤器参数。这实质上是使用$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;
}

直接相关实体代码的替代方法:

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

确保 inreverseedBy 变量与$className匹配才能正常工作