Skip to content

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

@Arkemlar

Description

@Arkemlar

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.

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