Suppose we have two entities ( Profile and Address ). A profile can have many addresses. In this case, we have:
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
Now, if I delete a profile that has many addresses:
$em->remove($profile);
How are addresses deleted?
Does Doctrine receive all the addresses associated with this profile and then delete them or only delete the profile and allow the database to handle the addresses?
I found some answers regarding Hibernate , but nothing about Doctrine .
EDIT : Add Three Notes From Doctrine Book
1.If the association is marked as CASCADE = REMOVE, Doctrine 2 will receive this association. If his single association passes this object to EntityManager#remove() . If the association is a collection, Doctrine will iterate over all its elements and pass them to EntityManager#remove() . In both cases, the semantics of cascade removal are applied recursively. For large object graphs, this deletion strategy can be very expensive.
2. Using the DQL DELETE operator allows you to delete several objects of the type with a single command and without wetting these objects. This can be very effective for removing large object graphs from the database.
3. Using the foreign key semantics onDelete = "CASCADE" can force the database to delete all related objects inside. This strategy is a bit complicated to get right, but can be very powerful and fast. You should know, however, that using strategy 1 ( CASCADE=REMOVE ) completely bypasses any foreign key onDelete=CASCADE option , because Doctrine will retrieve and delete all related objects explicitly nonetheless.
php symfony doctrine doctrine2
ghanbari
source share