微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – orphanRemoval绕过onDelete吗?

假设我们有两个实体(配置文件和地址).配置文件可以包含许多地址.在这种情况下,我们有:
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.

我做了一个测试:

我首先获取配置文件(只有配置文件没有连接)并将其传递给$em->删除($profile),然后doctrine运行另一个查询,获取与Profile(一个查询)相关的所有地址,然后在它之后,doctrine运行一个删除与Profile相关的每个地址的查询,并在最后删除Profile.

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

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

和orphanRemoval绕过onDelete.

原文地址:https://www.jb51.cc/php/134173.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐