Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

soyuka
Copy link
Contributor

@soyuka soyuka commented Sep 4, 2025

Fix #874

Breaking change in Symfony symfony/symfony#38662

@GromNaN GromNaN added the bug label Sep 4, 2025
@GromNaN GromNaN added this to the 5.4.1 milestone Sep 4, 2025
@GromNaN GromNaN changed the title fixes https://github.com/doctrine/DoctrineMongoDBBundle/issues/874 Fix UniqueValidator with new identifierFieldNames parameter Sep 4, 2025
@GromNaN GromNaN changed the base branch from 5.5.x to 5.4.x September 4, 2025 12:02
@GromNaN GromNaN requested a review from malarzm September 4, 2025 12:52
@GromNaN
Copy link
Member

GromNaN commented Sep 4, 2025

I completely revised the implementation. In fact, we don't have to override the constructor to change only the default value of the $service parameter. We only need to redeclare the default value in the $service property.

We have to declare the class conditionally to keep the compatibility with Symfony 6.4 as the prop type has been added in 7.0.

Property Doctrine\Bundle\MongoDBBundle\Validator\Constraints\Unique::$service overriding property
Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity::$service (string) should also have native type
string.

https://github.com/phpstan/phpstan-src/blob/f4e4354045c2ed972ff3c4ad927fcad539a8a338/src/Rules/Properties/OverridingPropertyRule.php#L168-L176
#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
class Unique extends UniqueEntity
{
if ((new ReflectionProperty(UniqueEntity::class, 'service'))->hasType()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted that this is essentially a test for Symfony 7.0+ based on symfony/symfony@7ea2461 adding parameter types to UniqueEntity.

@@ -23,11 +25,71 @@ public function testWithDefaultProperty(): void
self::assertInstanceOf(Unique::class, $constraint);
self::assertSame(['email'], $constraint->fields);
self::assertSame('doctrine_odm.mongodb.unique', $constraint->validatedBy());
self::assertNull($constraint->em);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not questioning the correctness here, but why was a $constraint->em assertion introduced?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hit an issue while working on the previous implementation.

Copy link
Member

@GromNaN GromNaN Sep 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would have been so nice if this parameter was named manager to take into account the ODM.

@GromNaN GromNaN merged commit f3baab4 into doctrine:5.4.x Sep 4, 2025
17 of 18 checks passed
@GromNaN
Copy link
Member

GromNaN commented Sep 4, 2025

Thank you @soyuka for raising the issue again and proposing a solution.

@soyuka
Copy link
Contributor Author

soyuka commented Sep 5, 2025

thanks @GromNaN for taking this over my dirty patch :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unique validator does not work when validation groups are used
3 participants