Doctrine2 ManyToMany relationships do not preserve

I have a problem with my ManyToMany attitude in doctrine2. The relationship is not maintained, even if the relationship exists. If I check that they are stored in two foreach loops, the correct objects are returned.

The first class is the Document.

class Document extends BaseEntity { .... /** * @ORM\ManyToMany(targetEntity="Job", mappedBy="documents", cascade={"all"}) * @ORM\JoinTable(name="job_document") */ protected $jobs; .... 

Second grade - Job

 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(); 

Add functions work fine. I can see this when I scroll through objects, and I do it.

0
source share
1 answer

It seems to me that you are only trying to update the reverse side, and not belonging to it.

As stated in the documentation of the doctrine :

Changes made only to the reverse side of the association are ignored. Be sure to update both sides of the bidirectional communication (or least owned, in terms of Doctrines)

0
source

Source: https://habr.com/ru/post/1272822/


All Articles