[Feature] Ablity to write custom change detection logic for custom doctrine types #7496

Open
opened 2026-01-22 15:52:24 +01:00 by admin · 2 comments
Owner

Originally created by @Arkemlar on GitHub (Apr 16, 2025).

Feature Request

What

Currently, during changeset computation, instances of custom types are compared by === comparison.
My proposal makes it possible to override this behavior. To do so, there must be a way to define a function that does only one thing: compares two values before-and-after.

Why

I seen lots of questions from doctrine users in git hub and stackoverflow like "why changes not detected when I use custom doctrine type?".
And the only answer was: override entity's property with new instance. Well, that's good and correct when we use value objects. But in other cases this is kind of rude way to do things, I think. And sometimes it is painful to write lots of extra code. So my approach reduces pain.

How

I see multiple ways to implement it:

  1. When I write custom type class with convertToPHPValue and convertToDatabaseValue methods, I could also write compareValues it will be called instead of using === for comparison. Example:
class SomeCustomType extends Type
{
    public function convertToPHPValue($value, AbstractPlatform $platform) {}
    public function convertToPHPValue($value, AbstractPlatform $platform) {}
    public function compareValue($valueA, $valueB) {
        return $valueA == $valueB;
    }
}
  1. Another way is to add extra field to ORM\Column annotation where I can define comparator - reference to a static function, like so:
    #[ORM\Column(comparator: [SomeClass::class, 'compare'])]
    private SomeCustomType $field;

My personal like to first one by many means.

Originally created by @Arkemlar on GitHub (Apr 16, 2025). ### Feature Request #### What Currently, during changeset computation, instances of custom types are compared by `===` comparison. My proposal makes it possible to override this behavior. To do so, there must be a way to define a function that does only one thing: compares two values before-and-after. #### Why I seen lots of questions from doctrine users in git hub and stackoverflow like "why changes not detected when I use custom doctrine type?". And the only answer was: override entity's property with new instance. Well, that's good and correct when we use value objects. But in other cases this is kind of rude way to do things, I think. And sometimes it is painful to write lots of extra code. So my approach reduces pain. #### How I see multiple ways to implement it: 1) When I write custom type class with `convertToPHPValue` and `convertToDatabaseValue` methods, I could also write `compareValues` it will be called instead of using `===` for comparison. Example: ```php class SomeCustomType extends Type { public function convertToPHPValue($value, AbstractPlatform $platform) {} public function convertToPHPValue($value, AbstractPlatform $platform) {} public function compareValue($valueA, $valueB) { return $valueA == $valueB; } } ``` 2) Another way is to add extra field to `ORM\Column` annotation where I can define comparator - reference to a static function, like so: ```php #[ORM\Column(comparator: [SomeClass::class, 'compare'])] private SomeCustomType $field; ``` My personal like to first one by many means.
Author
Owner

@Arkemlar commented on GitHub (Apr 17, 2025):

If it's fine, I might implement it.

@Arkemlar commented on GitHub (Apr 17, 2025): If it's fine, I might implement it.
Author
Owner

@arno14 commented on GitHub (Jun 30, 2025):

See https://github.com/doctrine/orm/issues/5542#issuecomment-2521108623

@arno14 commented on GitHub (Jun 30, 2025): See https://github.com/doctrine/orm/issues/5542#issuecomment-2521108623
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#7496