2011-04-26 2 views
4

I 해요 내가관계 교리를 삭제

같은 정의 된 사용자 및 카테고리 클래스, 사용자가 여러 카테고리에 속할 수 있습니다 ..이에 대한 교리에 완전히 새로운, 그래서 .. 저와 함께 간단한에서

베어하시기 바랍니다 사용자에 대한 카테고리를 추가 는

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

내가 수있을 것 않았다 만약 내가 예를 들어 ... 특정 관계를 제거 할 방법 문서의에서 볼 수 없습니다 삭제 관계, 내가 제거를 사용하여 엔터티를 삭제하면 전체 범주가 제거됩니까? 어떤 단서를 높이 평가!

답변

6

참조 가이드에서 Working with Associations을 확인해야합니다. 이것을 설명합니다.

<?php 

class Application_Entity_User 
{ 

      // ... snipped for brevity 

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

}