mirror of
https://github.com/doctrine/orm.git
synced 2026-03-24 06:52:09 +01:00
Emptying collection containing Single-Inheritence Discriminated Entity uses dangerous DELETE statement #7386
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @gitbugr on GitHub (Jun 14, 2024).
Bug Report
Summary
Emptying collection containing Single-Inheritence Discriminated Entity uses a dangerous DELETE statements that can lead to unintentionally removed records for different entities within the same single table inheritance.
Current behavior
When an Entity
UserAhas a propertythingswhich is a OneToMany relation with orphanRemoval enabled to an EntityThingA(referencesUserAvia a property ofowner) which is part of a single table hierarchy (using discriminator mapping) extending fromAbstractThing, doing the following:$userA->getThings()->clear();and persisting+flushing causes the db to receive iterated DELETE statements for the records in the Collection (e.g.
DELETE FROM things WHERE id = 1; DELETE FROM things WHERE id = 2; #... etc.)If instead you do:
and persist+flush, then the database instead receives a request of the form
DELETE FROM things WHERE owner_id = 1;, without the discriminator column in the WHERE clause.This can cause a problem in the instance where another Entity in the same hierarchy,
ThingB, has an association to a different entity,UserB, using the same property name since this could lead to collisions in theUserA/UserBowned records leading to entities from one being removed due to the deletion of those associated with the other.How to reproduce
Minimal reproducible example: https://github.com/gitbugr/doctrine-assoc-delete-example
Expected behavior
I would expect the delete directive to include the discriminator column in addition to the id. Or, if
$userA->setThings(new ArrayCollection());is improper, it should be guarded against.