orphanRemoval 会绕过 onDelete 吗?
Does orphanRemoval bypass onDelete?
假设我们有两个实体(Profile
和 Address
)。一个配置文件可以有多个地址。在这种情况下,我们有:
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 book
的三个注释
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.
orphan removal in the Doctrine manual. There is also another question 中有一节澄清 onDelete: cascade
。希望这两个链接能帮助您进一步理解该主题。
此外,确保在 $em->remove($profile);
之后调用 $em->flush();
操作将本地工作单元同步到数据库。
我做一个测试:
我先获取Profile
(只有profile
没有join)然后传给$em->remove($profile)
,然后doctrine 运行 另一个查询,它获取与 Profile
(一个查询)相关的所有 Address
,然后是 doctrine 运行 删除查询与 Profile
相关的每个地址 ,最后删除 Profile
.
所以,我可以说 orphanRemoval 是另一种类型的级联,正如学说所说:
orphanRemoval: There is another concept of cascading that is relevant only when removing entities from collections
和orphanRemoval
绕过onDelete
。
假设我们有两个实体(Profile
和 Address
)。一个配置文件可以有多个地址。在这种情况下,我们有:
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 book
的三个注释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 keyonDelete=CASCADE
option, because Doctrine will fetch and remove all associated entities explicitly nevertheless.
orphan removal in the Doctrine manual. There is also another question 中有一节澄清 onDelete: cascade
。希望这两个链接能帮助您进一步理解该主题。
此外,确保在 $em->remove($profile);
之后调用 $em->flush();
操作将本地工作单元同步到数据库。
我做一个测试:
我先获取Profile
(只有profile
没有join)然后传给$em->remove($profile)
,然后doctrine 运行 另一个查询,它获取与 Profile
(一个查询)相关的所有 Address
,然后是 doctrine 运行 删除查询与 Profile
相关的每个地址 ,最后删除 Profile
.
所以,我可以说 orphanRemoval 是另一种类型的级联,正如学说所说:
orphanRemoval: There is another concept of cascading that is relevant only when removing entities from collections
和orphanRemoval
绕过onDelete
。