Cannot assign null to property App\Entity\X::$id of type int #6618

Open
opened 2026-01-22 15:35:49 +01:00 by admin · 4 comments
Owner

Originally created by @BenMorel on GitHub (Feb 4, 2021).

I'm using typed properties in my entities. Each entity has a non-nullable ID:

/**
 * @ORM\Entity
 */
class X
{
    private int $id;
}

If you attempt to access $id before it's initialized (before it's flushed to the DB by Doctrine), you get an Error. I'm perfectly happy with that.

The only issue I can see is when I attempt to remove the entity:

$em->remove($entity);
$em->flush();

This fails with an exception:

Cannot assign null to property App\Entity\X::$id of type int

The obvious fix is to make the ID nullable:

private ?int $id;

But it really is a band aid. Questions:

  • Why does Doctrine need to set the ID to null when an entity is removed? Isn't removing it from the identity map and making it detached enough?
  • Would you accept a PR that prevents making it null on remove?
  • If so, should we do so only for typed properties, or for all properties for consistency?
Originally created by @BenMorel on GitHub (Feb 4, 2021). I'm using typed properties in my entities. Each entity has a non-nullable ID: ```php /** * @ORM\Entity */ class X { private int $id; } ``` If you attempt to access `$id` before it's initialized (before it's flushed to the DB by Doctrine), you get an `Error`. I'm perfectly happy with that. The only issue I can see is when I attempt to remove the entity: ```php $em->remove($entity); $em->flush(); ``` This fails with an exception: > Cannot assign null to property App\Entity\X::$id of type int The obvious fix is to make the ID nullable: ```php private ?int $id; ``` But it really is a band aid. Questions: - Why does Doctrine need to set the ID to `null` when an entity is removed? Isn't removing it from the identity map and making it detached enough? - Would you accept a PR that prevents making it null on remove? - If so, should we do so only for typed properties, or for all properties for consistency?
Author
Owner

@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.

@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.
Author
Owner

@BenMorel commented on GitHub (Feb 7, 2021):

I'm using the latest versions of all of the packages:

  • orm: 2.8.1
  • persistence: 2.1.0
  • common: 3.1.1
@BenMorel commented on GitHub (Feb 7, 2021): I'm using the latest versions of all of the packages: - `orm`: 2.8.1 - `persistence`: 2.1.0 - `common`: 3.1.1
Author
Owner

@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): ~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.
Author
Owner

@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:

$em->remove($entity);
$em->flush();

$this->addFlash('success', 'Entity with ID ' . $entity->getId() . ' deleted');
// When you are not using typed properties, the ID property becomes null and the getId():int function fails:
// getId() must be of the type int, null returned

// or when you use typed non null property you'll get:
// $id must not be accessed before initialization

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.

@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: ```php $em->remove($entity); $em->flush(); $this->addFlash('success', 'Entity with ID ' . $entity->getId() . ' deleted'); // When you are not using typed properties, the ID property becomes null and the getId():int function fails: // getId() must be of the type int, null returned // or when you use typed non null property you'll get: // $id must not be accessed before initialization ``` 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.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6618