mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
Bug: UnitOfWork try to set null to a read-only id column after remove #6936
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 @viniciusss on GitHub (Feb 22, 2022).
Bug Report
Summary
I have a entity with a read-only id property, and when a try to remove the entry a receive a LogicException.
Attempting to change readonly property App\Entity\Supplier::$id.Current behavior
The UnitOfWork always try to set the id of entity to null when it' executes the
executeDeletionsmethod.ed50e3d967/lib/Doctrine/ORM/UnitOfWork.php (L1254-L1256)How to reproduce
Expected behavior
Delete the entry from database, but i dont know what's the expected behavior to the orm internals.
@Arkemlar commented on GitHub (Dec 20, 2023):
For those who run on same issue there is a trick that helps to keep code clean.
If you use autoincrement Id then you anyway need a field that might be nullable and changeable.
So, create an Id class:
Complete file here
And then use it in your class:
So in your entity you have readonly field and that's fine. And this also correponds to DDD practices, especially if you create custom id class and use it instead of
AutoincrementIdin your entity:The only drawback is that
AutoincrementId(or custom id) is not a 100% ValueObject since it is changeable, but it is changeable by doctrine only so it is sort of compromise.This techinque works perfectly in my project.