Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Reflection] Use default typed property value when setting value to null #160

Closed
wants to merge 1 commit into from

Conversation

ruudk
Copy link

@ruudk ruudk commented Mar 10, 2021

Related to doctrine/orm#7999

I'm using typed properties with a default value that is not null. Example:

class Entity {
    /**
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private int $id = 0;

    public function getId() : int
    {
        return $this->id;
    }
}

This works great. When inserting a new entity in the database, the auto incremented id is updated properly.

It means that entities that are not persisted will have a 0 as id and the rest will have another non-zero integer value.

But upon deleting an entity this becomes a problem. The ORM tries to set the id property value to null and that causes a TypeError:

TypeError: Typed property Entity::$id must be int, null used

vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:1246
vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:441
vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:376

With this change applied, the default value of the id property 0 is used again.

Solving the problem.

I'm wondering: Does this make any sense? Is this crazy? Am I overlooking something, or is this the solution? 馃槉

Related to doctrine/orm#7999

I'm using typed properties with a default value that is not null. Example:
```php
class Entity {
    /**
     * @Orm\Column(name="id", type="integer")
     * @Orm\Id
     * @Orm\GeneratedValue(strategy="AUTO")
     */
    private int $id = 0;

    public function getId() : int
    {
        return $this->id;
    }
}
```

This works great. When inserting a new entity in the database, the auto incremented id is updated properly.

It means that entities that are not persisted will have a `0` as id and the rest will have another non-zero integer value.

But upon deleting an entity this becomes a problem. The ORM tries to set the `id` property value to `null` and that causes a TypeError:
```
TypeError: Typed property Entity::$id must be int, null used

vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:1246
vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:441
vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:376
```

With this change applied, the default value of the `id` property `0` is used again.

Solving the problem.
@SenseException
Copy link
Member

Thanks for contributing your pull request. I'm not sure about the use case. What is the reason that you prefer to use a 0 valued id instead of a nullable type?

@stof
Copy link
Member

stof commented Mar 11, 2021

@SenseException I would say, to avoid making getId() return a nullable type when any persisted entity would never have null

@SenseException
Copy link
Member

In case this is about a check if the entity has an id, 0 !== $entity->getId() can be handled internally in the entity itself with $entity->hasId() or something similar.

@ruudk
Copy link
Author

ruudk commented Apr 20, 2021

Closing as I lost interesting in solving it this way.... Thanks for the feedback though.

@ruudk ruudk closed this Apr 20, 2021
@ruudk ruudk deleted the reset-to-default-value branch April 20, 2021 10:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants