Content-Length: 278098 | pFad | http://github.com/symfony/symfony/pull/61224

85 [WIP][DoctrineBridge] Add `comparator` option to `UniqueEntity` constraint by blibio · Pull Request #61224 · symfony/symfony · GitHub
Skip to content

[WIP][DoctrineBridge] Add comparator option to UniqueEntity constraint #61224

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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: 7.4
Choose a base branch
from

Conversation

blibio
Copy link

@blibio blibio commented Jul 24, 2025

Q A
Branch? 7.4
Bug fix? no
New feature? yes
Deprecations? no
Issues ~
License MIT

This pull request adds a new configuration option comparator to the UniqueEntity constraint. The value must be a callable and works instead of identifierFieldNames. The callback, if supplied, is called instead of working through identifierFieldNames, to establish equality between the found entity and the data on the validated object.
This allows custom behavior in those edge-cases where the default equality check after stringification is not enough.

The following is an example of how this works:

#[UniqueEntity(
    fields: ['locale', 'urlAlias'],
    entityClass: EntityTranslation::class,
    // `identifierFieldNames` cannot be used, as, even though the `root` entity
    // would be found, the equality check of `rootId === (string) root` would fail.
    // identifierFieldNames: [
    //     'rootId' => 'root',
    //     'locale' => 'locale',
    // ],
    // instead a `comparator` can be supplied to handle the edge-case:
    comparator: [UpdateEntityTranslationFormData::class, 'compare']
)]
final class UpdateEntityTranslationFormData
{
    public function __construct(
        public readonly Uuid $rootId,

        public readonly ValueObject\Locale $locale,

        #[NotBlank]
        public ?string $name = null,

        #[NotBlank]
        public ?string $urlAlias = null,
    ) {}

    public static function compare(self $value, EntityTranslation $foundEntity): bool
    {
        return $foundEntity->getRoot()->getId()->equals($value->rootId)
            && $foundEntity->getLocale()->equals($value->locale);
    }
}

Without such functionality, one currently has two options:

  1. Assign the "full" entity to the DTO, instead of just the id.
  2. Have that entity implement \Stringable.

Both are problematic in some cases:

  • Solution 1. is not a workable option if one deals with DTOs that should be serializable (for example CQRS commands)
  • Solution 2. seems like a hack and issn't really semantic in most cases. Why should an entity be stringable to its id?

Todos:

  • probably add more tests
  • submit changes to the documentation
  • add entry to CHANGELOG
  • gather feedback for my changes

This being my first contribution I'm aware that I probably missed a few guidelines - though I tried to follow https://symfony.com/doc/current/contributing/code/pull_requests.html closely. Please don't hesitate to point those out, or if this functionality is in general not wanted!

… and enforce use of only `identifierFieldNames` or `comparator`

The `comparator` allows the `UniqueEntityValidator` to delegate the equality check to a user-defined callback. This helps in edge-cases where a simple equality check (after casting to string) of all `identifierFieldNames` is not enough.
@carsonbot
Copy link

Hey!

I see that this is your first PR. That is great! Welcome!

Symfony has a contribution guide which I suggest you to read.

In short:

  • Always add tests
  • Keep backward compatibility (see https://symfony.com/bc).
  • Bug fixes must be submitted against the lowest maintained branch where they apply (see https://symfony.com/releases)
  • Features and deprecations must be submitted against the 7.4 branch.

Review the GitHub status checks of your pull request and try to solve the reported issues. If some tests are failing, try to see if they are failing because of this change.

When two Symfony core team members approve this change, it will be merged and you will become an official Symfony contributor!
If this PR is merged in a lower version branch, it will be merged up to all maintained branches within a few days.

I am going to sit back now and wait for the reviews.

Cheers!

Carsonbot

@blibio blibio changed the title [DoctrineBridge] Add comparator option to UniqueEntity constraint [WIP][DoctrineBridge] Add comparator option to UniqueEntity constraint Jul 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/symfony/symfony/pull/61224

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy