さて、私はこれを最終的に機能させることができました。$parentAssociationMapping
コメントの親エンティティはThreadエンティティの具体的なインスタンスであるのに対し、この場合の親の'admin'クラスはStory(を介してリンクされている)であるため、CommentAdminクラスのプロパティを使用するメリットはありませんでした。 StoryThread)。さらに、他のタイプのエンティティにコメントを実装する場合は、これを動的に保つ必要があります。
まず、addChildメソッドを呼び出すようにStoryAdmin(およびCommentAdminを子として持つ他の管理クラス)を構成する必要がありました。
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;
}