mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
Cannot assign null to property App\Entity\X::$id of type int #6618
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 @BenMorel on GitHub (Feb 4, 2021).
I'm using typed properties in my entities. Each entity has a non-nullable ID:
If you attempt to access
$idbefore it's initialized (before it's flushed to the DB by Doctrine), you get anError. I'm perfectly happy with that.The only issue I can see is when I attempt to remove the entity:
This fails with an exception:
The obvious fix is to make the ID nullable:
But it really is a band aid. Questions:
nullwhen an entity is removed? Isn't removing it from the identity map and making it detached enough?@beberlei commented on GitHub (Feb 7, 2021):
What version of doctrine/persistence and reflection are you on? I vaguely remember we had and fixed this issue before.
@BenMorel commented on GitHub (Feb 7, 2021):
I'm using the latest versions of all of the packages:
orm: 2.8.1persistence: 2.1.0common: 3.1.1@ruudk commented on GitHub (Mar 10, 2021):
PR to fix this problem: https://github.com/doctrine/persistence/pull/160 when using a default value 0.This does not solve the problem the author is describing. It's a different but similar problem.
@ruudk commented on GitHub (Mar 10, 2021):
I think it's kind of weird that Doctrine clears the ID when the entity is removed. It makes code like this not work:
Interestingly enough it only clears this when the entity uses an id generator.
The fact that the ID has been removed from the database does not mean that the ID is not relevant anymore. For example, other parts of the system could be interested in the ID to clear their caches. Or to remove items from other databases that reference that ID.