加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – orphanRemoval绕过onDelete吗?

发布时间:2020-12-13 17:58:27 所属栏目:PHP教程 来源:网络整理
导读:假设我们有两个实体(配置文件和地址).配置文件可以包含许多地址.在这种情况下,我们有: Profile: ... fields: ... oneToMany: addresses: targetEntity: Address mappedBy: profile orphanRemoval: trueAddress: ... fields: ... manyToOne: profile: target
假设我们有两个实体(配置文件和地址).配置文件可以包含许多地址.在这种情况下,我们有:
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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读