简体   繁体   English

删除与学说的关联

[英]Removing associations with Doctrine

I'm using Doctrine and have three tables. 我正在使用Doctrine,有三个表。 artists , categories , artist_category . artistscategoriesartist_category I have an import script which imports artists, categories and the associations from a site and saves it into the database. 我有一个导入脚本,该脚本从站点导入艺术家,类别和关联并将其保存到数据库中。 The problem I'm having is removing an association. 我遇到的问题是删除关联。 Let's just imagine I have one artist and this artist is associated with three categories. 假设我有一位艺术家,并且该艺术家与三个类别相关联。 Now in the next import one artist_category association got removed. 现在,在下一个导入中,删除了artist_category关联。 What I want to do now is to remove the categories from the association table that aren't relevant anymore. 我现在想做的是从关联表中删除不再相关的类别。

Let's say my artist has the ID 254 and the category that should be removed has the ID 4. 假设我的艺术家的ID为254,应该删除的类别的ID为4。

My database table currently looks like this 我的数据库表当前看起来像这样

在此处输入图片说明

Now I have an array with all categories that the artist should be associated with 现在,我有了一个与艺术家应该关联的所有类别的数组

array(1) {
  [0]=>
  string(1) "3"
}

This would mean, that in my next step I have to remove the association artist_id (254) and artistcategory_id (4). 这意味着,在下一步中,我必须删除关联artist_id(254)和artistcategory_id(4)。

What I'm doing is this 我在做什么

foreach ($artist->getArtistCategories() as $artistCategory) {
    if (!in_array($artistCategory->getId(), $artistCategories)) {
        $artist->removeArtistCategories($artistCategory->getId());
    }
}

which is working. 这正在工作。 In the case of category 4 the method removeArtistCategories is called. 在类别4的情况下,将调用removeArtistCategories方法。 However, the deletion doesn't work. 但是,删除无效。 This is what I tried 这是我尝试过的

$this->artistCategories->removeElement($artistCategory);

returns false 返回假

$this->artistCategories->remove($artistCategory);

returns null 返回null

$this->artistCategories->contains($artistCategory)

returns false 返回假

However, when I var_dump($this->artistCategories) I'm getting pretty much information about the categories. 但是,当我var_dump($this->artistCategories)我得到了有关类别的很多信息。 Deletion however, doesn't work. 但是,删除不起作用。 What am I doing wrong? 我究竟做错了什么?

Doctrine works with entity objects, not with IDs. 原则适用于实体对象,而不适用于ID。 So instead: 所以与其:

$artist->removeArtistCategory($artistCategory->getId());

try 尝试

$artist->removeArtistCategory($artistCategory);

and define your remover like described in the Doctrine documentation: 并按照“ Doctrine”文档中的说明定义卸妆:

http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/working-with-associations.html#association-management-methods http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/working-with-associations.html#association-management-methods

public function removeArtistCategory(Category $category) {
    $this->artistCategories->removeElement($category);
}

Note that I renamed the method removeArtistCategories to removeArtistCategory as you just remove one entry and not multiple categories. 请注意,由于您只删除一个条目而不是多个类别,因此我将方法removeArtistCategories重命名为removeArtistCategory This is important because removers and adders have a naming convention just like getters and setters and you will have problems using some components for doctrine if you don't define your methods correctly. 这一点很重要,因为removers adders具有与getterssetters一样的命名约定,并且如果您未正确定义方法,则在使用某些组件进行学说时会遇到问题。

I just assumed you have a toMany relationship between Artist and Category because I don't know your entity definition, I'd need more information to give you a proper solution. 我只是假设您在ArtistCategory之间存在toMany关系,因为我不知道您的实体定义,我需要更多信息才能为您提供适当的解决方案。 So this code is untested and might not work! 因此,此代码未经测试,可能无法正常工作!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM