2011-04-26 91 views
4

我完全新的教義,所以請與我裸...刪除關係學說

在最簡單的,因爲我有一個用戶和類別類,用戶可以屬於多個類別..定義爲這樣

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; 
    } 
} 

添加類別的用戶很容易,但我不明白,或從文檔的看我怎麼會刪除特定關係......舉例來說,如果我做了

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

我能刪除e關係,如果我使用remove刪除實體,那麼整個類別將被刪除?任何線索讚賞!

回答

6

您應該從參考指南中檢查出Working with Associations。它解釋了這一點。

<?php 

class Application_Entity_User 
{ 

      // ... snipped for brevity 

    public function deleteCategory(Application_Entity_Category $category) 
    { 
     $this->userCategories->removeElement($category); 
    } 

}