php - 奏鸣曲管理包 : possible to add a child admin object that can have different parents?

标签 php symfony admin symfony-sonata sonata-admin

我正在使用原则继承映射来使各种对象链接到评论实体。这是通过各种具体的“线程”实现的,这些线程与评论具有一对多的关系。所以以'Story'元素为例,会有一个相关的'StoryThread'实体,它可以有很多评论。

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

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

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

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

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

最佳答案

好吧,我最终设法让它工作了。我无法从使用 CommentAdmin 类的 $parentAssociationMapping 属性中获益,因为评论的父实体是 Thread 实体的具体实例,而在本例中是父“admin”类是一个 Story(通过 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;
}

关于php - 奏鸣曲管理包 : possible to add a child admin object that can have different parents?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10605548/

相关文章:

php - symfony 4 - bugsnag - 忽略特定的异常类型

php - PHP 中类函数的类型转换

Symfony 4 - Webpack-encore 使用 FosJsRouting : Routing is not defined

django - 将 Django 管理表单放在不同的文件中

php - 具有多个字段时 $_FILES 数组的奇怪格式

php - 我如何在 codeigniter 中链接新页面

php - 如何将 PK 作为外键添加到现有表中?

php - Memcached "Could not connect"高峰时段错误

wordpress - 更新后 WP-Admin 中的 Javascript 失败

PHP - IF 语句普通用户或管理员有权访问