4

私は教義の継承マッピングを使用して、さまざまなオブジェクトをコメントエンティティにリンクできるようにしています。これは、コメントと1対多の関係を持つさまざまな具体的な「スレッド」によって実現されます。したがって、「Story」要素を例にとると、関連する「StoryThread」エンティティがあり、多くのコメントを含めることができます。

これはすべて正常に機能していますが、親エンティティの子として使用できるSonataAdminBundleのCommentAdminクラスを定義しようとして問題が発生しています。たとえば、次のようなルートを使用できるようにしたいと思います。

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

誰かが私がこれを達成するためにどのように行くことができるかについて何か指針を持っていますか?コードの抜粋を投稿したいのですが、関連するドキュメントを見つけることができなかったため、開始するのに最適な場所がわかりません。

投稿とコメントの間に同様の親/子管理者関係を実装しているため、 SonataNewsBundleを参照として使用しようとしていますが、これは「コメント」(子)管理者クラスにハードコーディングされているように見えますそれが投稿に属していることを知っており、親オブジェクトと直接多対1の関係を持っている必要があるようにも見えますが、私のものは別の「スレッド」エンティティを介しています。

これが理にかなっていることを願っています!助けてくれてありがとう。

4

2 に答える 2

16

さて、私はこれを最終的に機能させることができました。$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;
}
于 2012-05-16T12:04:06.507 に答える
1

直接関連するエンティティのコードの代替:

 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と一致していることを確認してください

于 2014-09-19T15:12:55.403 に答える