Metadata field type validation against Entity property type #7141

Closed
opened 2026-01-22 15:45:28 +01:00 by admin · 0 comments
Owner

Originally created by @DavideBicego on GitHub (Apr 27, 2023).

Feature Request

Q A
New Feature yes
RFC no
BC Break no

Summary

We found out that if the metadata field type differs from the entity property type Doctrine marks it as "to be updated" right after it being loaded from the database and hydrated, even if no real change happened to the entity. If there's a flush an update is performed for every entity marked as "to be updated", as it should happen.

For example if this is the metadata (Xml in this case)
MyEntity.xml:

<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping https://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
  <entity name="MyEntity" table="my_table">
    <id name="id" .../>
    <field name="a" type="boolean" column="a_column" nullable="false"/>
  </entity>
</doctrine-mapping>

MyEntity.php:

class MyEntity
{
    public function __construct(private int $a)
    {
    }
}

This code updates to the database

/** @var EntityManagerInterface $em */
$myEntity = $em->getRepository(MyEntity::class)->find($myEntityId);
$em->flush();

Please note that $a is int in MyEntity.php and boolean in MyEntity.xml

It would be great to have some strictness when setting the Entity value during Hydration or some field type validation in Doctrine\ORM\Tools\SchemaValidator.

Originally created by @DavideBicego on GitHub (Apr 27, 2023). ### Feature Request | Q | A |------------ | ------ | New Feature | yes | RFC | no | BC Break | no #### Summary We found out that if the metadata field type differs from the entity property type Doctrine marks it as "to be updated" right after it being loaded from the database and hydrated, even if no real change happened to the entity. If there's a flush an update is performed for every entity marked as "to be updated", as it should happen. For example if this is the metadata (Xml in this case) MyEntity.xml: ```xml <?xml version="1.0" encoding="utf-8"?> <doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping https://www.doctrine-project.org/schemas/orm/doctrine-mapping.xsd"> <entity name="MyEntity" table="my_table"> <id name="id" .../> <field name="a" type="boolean" column="a_column" nullable="false"/> </entity> </doctrine-mapping> ``` MyEntity.php: ```php class MyEntity { public function __construct(private int $a) { } } ``` This code updates to the database ```php /** @var EntityManagerInterface $em */ $myEntity = $em->getRepository(MyEntity::class)->find($myEntityId); $em->flush(); ``` Please note that $a is `int` in MyEntity.php and `boolean` in MyEntity.xml It would be great to have some strictness when setting the Entity value during Hydration or some field type validation in `Doctrine\ORM\Tools\SchemaValidator`.
admin closed this issue 2026-01-22 15:45:29 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#7141