本文介绍了Doctrine2 ManyToMany关系不保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对doctrine2中的ManyToMany关系有疑问.即使该关系存在,该关系也不会持久.如果我再次检查,则在两个foreach循环中保持不变,则返回正确的对象.
I have a problem with my ManyToMany relation in doctrine2. The relation doesn't persist even though the relation exists. If i check afther the persist in two foreach loops the correct objects are returned.
第一堂课是Document.
The first class is Document.
class Document extends BaseEntity
{
....
/**
* @ORM\ManyToMany(targetEntity="Job", mappedBy="documents", cascade={"all"})
* @ORM\JoinTable(name="job_document")
*/
protected $jobs;
....
第二堂课是乔布斯
class Job extends BaseEntity
{
....
/**
* @ORM\ManyToMany(targetEntity="Document", inversedBy="jobs", cascade={"all"})
* @ORM\JoinTable(name="job_document")
*/
protected $documents;
....
在我的控制器中,我执行以下操作:
In my controller I do the following:
$job->addDocument($document);
$document->addJob($job);
$em->persist($job);
$em->flush();
添加功能可以正常工作.当我再次遍历对象时,我可以看到它.
The add functions work fine. I can see it when I loop through the objects afther I do this.
推荐答案
在我看来,您仅尝试更新关系的相反方面,而不是关系的拥有方面.
It seems to me that you only try to update the inverse side and not the owning side of the relationship.
教义文档中指出:
这篇关于Doctrine2 ManyToMany关系不保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!