php – orphanRemoval绕过onDelete吗?

前端之家收集整理的这篇文章主要介绍了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.

猜你在找的PHP相关文章