2011-09-04 47 views
6

你如何取消鏈接從許多一對多表而不刪除任何的關係?學說2,如何刪除多對多關聯?

我曾嘗試:

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

$getProject->getCategory()->removeElement($getCat); 
$this->em->flush(); 

我Projectcat實體:

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

有人知道嗎?我試了一切我可以刪除該項目,它也將從多到多的表中刪除,但這次我只是想取消它們的鏈接。我需要它們存在但不與某個類別相關聯.. –

回答

8

你的信息是有點有限。關於數據庫方案和項目的一些額外信息會很好。但要試一試。

你必須從關係的雙方將其刪除。你從類別中刪除它,但你也應該從項目中刪除它。

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

祝你好運!

0

舊的文章,但上面的回答幫我,但它可能有助於擴大一點點,我有一個項目,可以有很多類別(比可以有很多項目類別),因此,這段代碼讓我所有的人:

$project->getCategories(); 

如果我想刪除所有類別的一個項目,我只是這樣做:

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

與原來的問題的問題是,我相信學說要你傳入項目引用的類別,而不僅僅是對引用的引用您使用此代碼獨立抓取的類別:

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

希望這是有道理的。我知道我搞混了術語。

+0

實體類不應該在乎是否使用了關聯的實體或引用。 http://doctrine-orm.readthedocs.org/en/latest/reference/advanced-configuration.html#reference-proxies代理引用對象應該是透明的。 – fyrye

6

一個相當古老的帖子,但希望提供一種方法來確保關聯已從教條的ORM實體一側移除,而不是必須手動執行每個實體的集合removeElement並擴展@Rene Terstegen的答案。

的問題是,原則不「自動神奇地」綁在一起的關聯,但是你可以更新實體的添加/刪除方法來做到這一點。

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

下面的例子是基於OP的項目/類別架構。 它假定表是ManyToMany關係表,並且projectcategory表使用主鍵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); 
    } 
} 

然後,所有你需要做的是調用removeCategoryremoveProject方法,而不是兩個。同樣可以應用於addCategoryaddProject方法爲好。

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