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 keyonDelete=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.