Doctrine 2, how to remove many of the many associations?

How do I unlink from a many-to-many table without deleting anything?

I tried:

$getProject = $this->_helper->getDocRepo('Entities\Project')->findOneBy(array('id' => $projectId)); $getCat = $this->_doctrine->getReference('\Entities\Projectcat', $catId); $getProject->getCategory()->removeElement($getCat); $this->em->flush(); 

my Projectcat object:

 /** * @ManyToMany(targetEntity="\Entities\Projectcat", cascade={"persist", "remove"}) * @JoinColumn(name="id", referencedColumnName="id") */ protected $getCategory; 
+6
source share
3 answers

Your information is a little limited. Some additional information about the database schema and design would be nice. But try it.

You must remove it on both sides of the relationship. You removed it from the category, but you must also remove it from the project.

 // Remove Category from Project $Project->Category->removeElement($Category); // Remove Project from Category $Category->Project->removeElement($Project); 

Good luck

+8
source

More likely an old post, but wanted to provide a way to ensure that the association was removed from the ORM Entity side of the doctrine, instead of manually executing each remove Entity element and extending the answer to @Rene Terstegen.

The problem is that Doctrine does not "automatically-magically" associate associations, however you can update the methods for adding / removing entities for this.

https://gist.github.com/Ocramius/3121916

The following example is based on the project design / OP category. The project_category table is assumed to be a ManyToMany relationship ManyToMany , and the project and category tables use the primary key id .

 class Project { /** * @ORM\ManyToMany(targetEntity="Category", inversedBy="projects") * @ORM\JoinTable( * name="project_category", * joinColumns={ * @ORM\JoinColumn(name="project", referencedColumnName="id") * }, * inverseJoinColumns={ * @ORM\JoinColumn(name="category", referencedColumnName="id") * } * ) */ protected $categories; public function __construct() { $this->categories = new ArrayCollection(); } /** * @param Category $category */ public function removeCategory(Category $category) { if (!$this->categories->contains($category)) { return; } $this->categories->removeElement($category); $category->removeProject($this); } } 

 class Category { /** * @ORM\ManyToMany(targetEntity="Project", mappedBy="categories") */ protected $projects; public function __construct() { $this->projects = new ArrayCollection(); } /** * @param Project $project */ public function removeProject(Project $project) { if (!$this->projects->contains($project)) { return; } $this->projects->removeElement($project); $project->removeCategory($this); } } 

Then you just need to call the removeCategory or removeProject , not both. The same can be applied to addCategory and addProject .

 $project = $em->find('Entities\Project', $projectId); $category = $em->getReference('Entities\Category', $categoryId); $project->removeCategory($category); $em->flush(); 
+6
source

Old post, but the answer above helped me, but it can help expand it a bit, I have a project that can have many categories (and categories than many projects can have), so this code gets me all of them:

 $project->getCategories(); 

If I wanted to remove all categories for the project, I just do this:

  foreach ($project->getCategories() as $category) { $project->getCategories()->removeElement($category); } 

The problem with the original question was that I believe Doctrine wants you to go to the category the project is referring to, and not just the link to the category that you grabbed independently with this code:

 $getCat = $this->_doctrine->getReference('\Entities\Projectcat', $catId); 

Hope this makes sense. I know that I confess a little terminology.

0
source

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


All Articles