2011-09-04 45 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();