Doctrine not persisting many to many relationship

Viewed 17

Hellow one and all.. I have an issue for which I have no explanation. The issue is that doctrine will not populate join table with data. So in my project, I have Document and Project and ManyToMany relationship between them.

Entity Document:

/**
 * @ORM\ManyToMany(targetEntity="App\Core\Doctrine\Entity\Project\Project", inversedBy="documents", cascade={"all"})
 * @ORM\JoinTable(name="document_project",
 *      joinColumns={@ORM\JoinColumn(name="document_id", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="project_id", referencedColumnName="id")}
 * )
 */
private ?Collection $projects;

inverse relationship is Project and there we have:

/**
 * @ORM\ManyToMany(targetEntity="App\Core\Doctrine\Entity\Document\Document", mappedBy="projects", cascade={"all"})
 */
private Collection $documents;

onece I start populating data I call fallowing method on Document entity:

public function addProject(Project $project): void
{
    if (!$this->projects->contains($project)) {
        $project->addDocument($this);
        $this->projects->add($project);
    }
}

and addDocument in Project looks like this:

public function addDocument(Document $document): void
{
    if (!$this->documents->contains($document)) {
        $this->documents->add($document);
    }
}

So if I dump data before persis then all looks good and document has one project for example. Next, I call persist and imidietly after I dump saved entity. Now projects is an empty ArrayCollection.

All help would be welcomed

0 Answers
Related