Remove Doctrine Relationship

I am completely unfamiliar with the Doctrine, so please tell me about me.

In my simplest case, I have a class "User" and "Category", a user can belong to several categories, defined as such

class Application_Model_User { public function __construct() { $this->userCategory = new ArrayCollection(); } /** * Unidirectional - Users have multiple categories they belong to * * @ManyToMany(targetEntity="Application_Model_Category") * @JoinTable(name="user_category", * joinColumns={@JoinColumn(name="user", referencedColumnName="id")}, * inverseJoinColumns={@JoinColumn(name="category", referencedColumnName="id")} * ) */ } private $userCategory; public function getUserCategories() { return $this->userCategory; } } 

Adding a category to a user is very simple, but I cannot understand or see from the document how I delete certain relationships ... For example, if I did

  $thing = $em->getRepository('Application_Model_User'); $result = $thing->findOneBy(array( 'id' => (int) 5 )); foreach($result->getUserCategories() as $category) { if($category->getName() == 'Another Sub Cat') { // Delete this relationship } } $em->flush(); 

I could be able to delete the link, if I delete the object with the deletion, is the entire category deleted? any tips appreciated!

+4
source share
1 answer

You should check Working with associations from the reference guide. This explains this.

 <?php class Application_Entity_User { // ... snipped for brevity public function deleteCategory(Application_Entity_Category $category) { $this->userCategories->removeElement($category); } } 
+6
source

All Articles