gpt4 book ai didi

symfony - Doctrine2 orphanRemoval ManyToMany 仅当没有其他记录连接时

转载 作者:行者123 更新时间:2023-12-02 21:22:42 25 4
gpt4 key购买 nike

我的目的是仅当实体连接音轨没有其他记录时才删除实体艺术家中的记录。

我尝试以这种方式使用 orphanRemoval:

Soundtrack.php

/**
* @Assert\NotBlank(message = "soundtrack.artists.blank")
* @ORM\ManyToMany(targetEntity="Artist", inversedBy="soundtrack", cascade={"persist", "remove"}, orphanRemoval=true)
* @ORM\JoinTable(name="soundtrack_artist")
* @ORM\OrderBy({"name" = "ASC"})
**/
private $artists;

Artist.php

/**
* @ORM\ManyToMany(targetEntity="Soundtrack", mappedBy="artists")
*/
private $soundtrack;

但是当我删除实体记录配乐时,也会清除实体艺术家的记录,即使它链接到其他记录配乐(我认为这就是您应该从 orphanRemoval 获得的结果)。

有没有办法仅在没有其他记录连接时删除该记录“孤立”?

我也尝试过这样:

**Soundtrack.php**

/**
* @Assert\NotBlank(message = "soundtrack.artists.blank")
* @ORM\ManyToMany(targetEntity="Artist", inversedBy="soundtrack", cascade={"persist"}, orphanRemoval=true)
* @ORM\JoinTable(name="soundtrack_artist")
* @ORM\OrderBy({"name" = "ASC"})
**/
private $artists;

但不删除记录实体艺术家..

最佳答案

orphanRemoval 选项明确认为,拥有方对象是引用其子对象的唯一实例。为了使其正常工作,您应该将子项与父项分离(未设置引用)以删除子项。对于多对多关联,您应该分离两侧的实体(拥有的和反向的)

参见Docs

When using the orphanRemoval=true option Doctrine makes the assumption that the entities are privately owned and will NOT be reused by other entities. If you neglect this assumption your entities will get deleted by Doctrine even if you assigned the orphaned entity to another one.

关于symfony - Doctrine2 orphanRemoval ManyToMany 仅当没有其他记录连接时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18577139/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com