gpt4 book ai didi

php - orphanRemoval 会绕过 onDelete 吗?

转载 作者:可可西里 更新时间:2023-11-01 12:37:33 24 4
gpt4 key购买 nike

假设我们有两个实体(ProfileAddress)。一个配置文件可以有多个地址。在这种情况下,我们有:

Profile:
...
fields:
...
oneToMany:
addresses:
targetEntity: Address
mappedBy: profile
orphanRemoval: true

Address:
...
fields:
...
manyToOne:
profile:
targetEntity: Profile
inversedBy: addresses
joinColumn:
name: profile_id
referencedColumnName: id
onDelete: cascade

现在,如果我删除包含多个地址的配置文件:

$em->remove($profile);

地址如何被删除?

Doctrine 是获取与此配置文件相关的所有地址然后删除它们,还是只删除配置文件并让数据库处理地址?

我找到了一些关于 Hibernate 的答案,但没有找到关于 Doctrine 的答案。


编辑:添加Doctrine书中的三个注释

1.If an association is marked as CASCADE=REMOVE Doctrine 2 will fetch this association. If its a Single association it will pass this entity to EntityManager#remove(). If the association is a collection, Doctrine will loop over all its elements and pass them toEntityManager#remove(). In both cases the cascade remove semantics are applied recursively. For large object graphs this removal strategy can be very costly.

2.Using a DQL DELETE statement allows you to delete multiple entities of a type with a single command and without hydrating these entities. This can be very efficient to delete large object graphs from the database.

3.Using foreign key semantics onDelete="CASCADE" can force the database to remove all associated objects internally. This strategy is a bit tricky to get right but can be very powerful and fast. You should be aware however that using strategy 1 (CASCADE=REMOVE) completely by-passes any foreign key onDelete=CASCADE option, because Doctrine will fetch and remove all associated entities explicitly nevertheless.

最佳答案

我做一个测试:

我首先获取 Profile(只有 profile 而没有 join )并将其传递给 $em->remove($profile) ,然后 doctrine 运行另一个查询,获取与 Profile 相关的所有 Address(一个查询),然后是 doctrine 对与 Profile 相关的每个地址运行删除查询,最后删除 Profile

所以,我可以说 orphanRemoval 是另一种类型的级联,正如 Doctrine 所说:

orphanRemoval: There is another concept of cascading that is relevant only when removing entities from collections

orphanRemoval绕过onDelete

关于php - orphanRemoval 会绕过 onDelete 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29723041/

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