Skip to content

Metadata field type validation against Entity property type #10661

@DavideBicego

Description

@DavideBicego

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions