【问题标题】:Doctrine2 : Detach / Merge on ManyToMany associationDoctrine2 : 在多对多关联上分离/合并
【发布时间】:2012-05-02 14:09:52
【问题描述】:

我一直在努力使用这些功能 (attach/merge feature)。

我的需求很简单,我有一个 BookFilterType 过滤 Book 对象的集合。

直到现在,什么时候做

public function indexAction()
{
    $book = new Book($this->getUser());
    $filters = $this->getFilters();
    $form = $this->createForm(new BookFilterType(), $filters);

    ...
    return array(...);
}

我收到一个错误Entities passed to the choice field must be managed,因为在过滤器中它们是其他实体。

所以基本上,在会话中保存过滤条件时,我只需要存储 ID,然后使用 find($id) 重新补充它们。

但这很难看,我的代码变得无法阅读!

所以我决定使用detach / merge 功能。

存储分离的实体后,我想再次合并它们以重新注入表单中的值,看来我需要在我的对象上使用cascade={"merge"},因为我得到了:

Class Doctrine\Common\Collections\ArrayCollection is not a valid entity or mapped super class.

问题(最后)

我应该把 cascade={"merge"} 放在哪里:在 joinColumnsinverseJoinColumns 上?或两者 ? ?因为我在这些过滤器中只有 ManyToMany 关联

/**
 * @ORM\ManyToMany(targetEntity="MyTargetedEntity")
 * @ORM\JoinTable(name="target_entity_nn",
 *      joinColumns={@ORM\JoinColumn(name="some_id", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="other_id", referencedColumnName="id")}
 *      )
 *
 **/
private $contributors;

PS:我正在使用Symfony2.1 / Doctrine master

谢谢

【问题讨论】:

    标签: symfony doctrine-orm


    【解决方案1】:
    猜你喜欢
    • 1970-01-01
    • 2017-10-24
    • 1970-01-01
    • 2013-01-29
    • 2011-09-24
    • 1970-01-01
    • 1970-01-01
    • 2012-08-09
    • 1970-01-01
    相关资源
    最近更新 更多