Customer
是“关键字/客户”的关系反侧Keyword
:在Doctrine 2中更新(从反面)双向多对多关系?
/**
* @ORM\ManyToMany(targetEntity="Keyword", mappedBy="customers",
* cascade={"persist", "remove"}
*)
*/
protected $keywords;
当创建一个新的客户,应该选择一个或多个关键词。实体表单字段是:
$form->add($this->factory->createNamed('entity', 'keywords', null, array(
'class' => 'Acme\HelloBundle\Entity\Keyword',
'property' => 'select_label',
'multiple' => true,
'expanded' => true,
)));
在我的控制器代码,之后绑定请求,并检查的形式是有效的,我需要坚持两个客户和所有客户/关键字关联,那就是加入表。
但是客户是反方,所以这是行不通的:
if($request->isPost()) {
$form->bindRequest($request);
if(!$form->isValid()) {
return array('form' => $form->createView());
}
// Valid form here
$em = $this->getEntityManager();
$em->persist($customer);
$em->flush();
}
事件与“级联”选项,该代码失败。 $customer->getKeywords()
将返回Doctrine\ORM\PersistentCollection
,其中只保存选定的关键字。
我应该手动检查哪个关键字被删除/添加,然后从拥有方更新?
我发布了我的解决方案[此处] [1]。希望它会有所帮助。 [1]:http://stackoverflow.com/a/27113108/3133441 – ajtamwojtek