Bidirection ManyToMany that only works one way #6944

Closed
opened 2026-01-22 15:41:57 +01:00 by admin · 0 comments
Owner

Originally created by @toto33331 on GitHub (Mar 8, 2022).

Hi,
I hope I post on the good place. I have a bidirectional ManyToMany that only works on one side (the owning side ? IDK)
Here is some code to explain:

class GlobalContent :

    /**
    * @ORM\ManyToMany(targetEntity="App\Entity\Groupement", inversedBy="all_global_contents")
    * @ORM\JoinTable(name="global_content_groupement")
    */
    private $all_groupements;

    /**
     * @return Collection|Groupement[]
     */
    public function getAllGroupements(): Collection
    {
        return $this->all_groupements;
    }

    public function addAllGroupement(Groupement $groupement): self
    {
        if (!$this->all_groupements->contains($groupement)) {
            $this->all_groupements[] = $groupement;
        }

        return $this;
    }

    public function removeAllGroupement(Groupement $groupement): self
    {
        if ($this->all_groupements->contains($groupement)) {
            $this->all_groupements->removeElement($groupement);
        }

        return $this;
    }

class Groupement:

/**
* @ORM\ManyToMany(targetEntity="App\Entity\GlobalContent", mappedBy="all_groupements")
*/
private $all_global_contents;

    /**
     * @return Collection|GlobalContent[]
     */
    public function getAllGlobalContents(): Collection
    {
        return $this->all_global_contents;
    }

    public function addAllGlobalContent(GlobalContent $global_content): self
    {
        if (!$this->all_global_contents->contains($global_content)) {
            $this->all_global_contents[] = $global_content;
        }
        return $this;
    }

    public function removeAllGlobalContent(GlobalContent $global_content): self
    {
        if ($this->all_global_contents->contains($global_content)) {
            $this->all_global_contents->removeElement($global_content);
        }
        return $this;
    }

This code as it is only works if I add/remove some Groupment from a GlobalContent object. From a Groupment object, I can get all the related GlobalContent objects without any problem.

But if I switch the sides (inversedBy + JoinTable and MappedBy) the code only works the other way, if I add/remove some GlobalContent from a Groupment object.

Any suggestion ? I've tried a shitload of things and nothing worked

Originally created by @toto33331 on GitHub (Mar 8, 2022). Hi, I hope I post on the good place. **I have a bidirectional ManyToMany that only works on one side** (the owning side ? IDK) Here is some code to explain: class GlobalContent : ``` /** * @ORM\ManyToMany(targetEntity="App\Entity\Groupement", inversedBy="all_global_contents") * @ORM\JoinTable(name="global_content_groupement") */ private $all_groupements; /** * @return Collection|Groupement[] */ public function getAllGroupements(): Collection { return $this->all_groupements; } public function addAllGroupement(Groupement $groupement): self { if (!$this->all_groupements->contains($groupement)) { $this->all_groupements[] = $groupement; } return $this; } public function removeAllGroupement(Groupement $groupement): self { if ($this->all_groupements->contains($groupement)) { $this->all_groupements->removeElement($groupement); } return $this; } ``` class Groupement: ``` /** * @ORM\ManyToMany(targetEntity="App\Entity\GlobalContent", mappedBy="all_groupements") */ private $all_global_contents; /** * @return Collection|GlobalContent[] */ public function getAllGlobalContents(): Collection { return $this->all_global_contents; } public function addAllGlobalContent(GlobalContent $global_content): self { if (!$this->all_global_contents->contains($global_content)) { $this->all_global_contents[] = $global_content; } return $this; } public function removeAllGlobalContent(GlobalContent $global_content): self { if ($this->all_global_contents->contains($global_content)) { $this->all_global_contents->removeElement($global_content); } return $this; } ``` This code as it is only works if I add/remove some Groupment from a GlobalContent object. From a Groupment object, I can get all the related GlobalContent objects without any problem. **But if I switch the sides (inversedBy + JoinTable and MappedBy) the code only works the other way,** if I add/remove some GlobalContent from a Groupment object. Any suggestion ? I've tried a shitload of things and nothing worked
admin closed this issue 2026-01-22 15:41:57 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6944