Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite80f5d7

Browse files
committed
bug#60275 [DoctrineBridge] Fix UniqueEntityValidator Stringable identifiers (GiuseppeArcuti, wkania)
This PR was merged into the 7.2 branch.Discussion----------[DoctrineBridge] Fix UniqueEntityValidator Stringable identifiers| Q | A| ------------- | ---| Branch? | 7.2| Bug fix? | yes| New feature? | no| Deprecations? | no| Issues |Fix#58883| License | MIT`@GiuseppeArcuti` , thanks for creating the test in [PR](#59126).More info in the [comment](#59126 (comment)).Commits-------b0f012f [DoctrineBridge] Fix UniqueEntityValidator Stringable identifiers572ebe8 [DoctrineBridge] Fix UniqueEntity for non-integer identifiers
2 parents310f769 +b0f012f commite80f5d7

File tree

4 files changed

+84
-0
lines changed

4 files changed

+84
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespaceSymfony\Bridge\Doctrine\Tests\Fixtures;
13+
14+
useSymfony\Component\Uid\Uuid;
15+
16+
class UserUuidNameDto
17+
{
18+
publicfunction__construct(
19+
public ?Uuid$id,
20+
public ?string$fullName,
21+
public ?string$address,
22+
) {
23+
}
24+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespaceSymfony\Bridge\Doctrine\Tests\Fixtures;
13+
14+
useDoctrine\ORM\Mapping\Column;
15+
useDoctrine\ORM\Mapping\Entity;
16+
useDoctrine\ORM\Mapping\Id;
17+
useSymfony\Component\Uid\Uuid;
18+
19+
#[Entity]
20+
class UserUuidNameEntity
21+
{
22+
publicfunction__construct(
23+
#[Id, Column]
24+
public ?Uuid$id =null,
25+
#[Column(unique:true)]
26+
public ?string$fullName =null,
27+
) {
28+
}
29+
}

‎src/Symfony/Bridge/Doctrine/Tests/Validator/Constraints/UniqueEntityValidatorTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,12 @@
4141
useSymfony\Bridge\Doctrine\Tests\Fixtures\UpdateCompositeIntIdEntity;
4242
useSymfony\Bridge\Doctrine\Tests\Fixtures\UpdateCompositeObjectNoToStringIdEntity;
4343
useSymfony\Bridge\Doctrine\Tests\Fixtures\UpdateEmployeeProfile;
44+
useSymfony\Bridge\Doctrine\Tests\Fixtures\UserUuidNameDto;
45+
useSymfony\Bridge\Doctrine\Tests\Fixtures\UserUuidNameEntity;
4446
useSymfony\Bridge\Doctrine\Tests\TestRepositoryFactory;
4547
useSymfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
4648
useSymfony\Bridge\Doctrine\Validator\Constraints\UniqueEntityValidator;
49+
useSymfony\Component\Uid\Uuid;
4750
useSymfony\Component\Validator\Exception\ConstraintDefinitionException;
4851
useSymfony\Component\Validator\Exception\UnexpectedValueException;
4952
useSymfony\Component\Validator\Test\ConstraintValidatorTestCase;
@@ -116,6 +119,7 @@ private function createSchema($em)
116119
$em->getClassMetadata(Employee::class),
117120
$em->getClassMetadata(CompositeObjectNoToStringIdEntity::class),
118121
$em->getClassMetadata(SingleIntIdStringWrapperNameEntity::class),
122+
$em->getClassMetadata(UserUuidNameEntity::class),
119123
]);
120124
}
121125

@@ -1401,4 +1405,25 @@ public function testEntityManagerNullObjectWhenDTODoctrineStyle()
14011405

14021406
$this->validator->validate($dto,$constraint);
14031407
}
1408+
1409+
publicfunctiontestUuidIdentifierWithSameValueDifferentInstanceDoesNotCauseViolation()
1410+
{
1411+
$uuidString ='ec562e21-1fc8-4e55-8de7-a42389ac75c5';
1412+
$existingPerson =newUserUuidNameEntity(Uuid::fromString($uuidString),'Foo Bar');
1413+
$this->em->persist($existingPerson);
1414+
$this->em->flush();
1415+
1416+
$dto =newUserUuidNameDto(Uuid::fromString($uuidString),'Foo Bar','');
1417+
1418+
$constraint =newUniqueEntity(
1419+
fields: ['fullName'],
1420+
entityClass: UserUuidNameEntity::class,
1421+
identifierFieldNames: ['id'],
1422+
em:self::EM_NAME,
1423+
);
1424+
1425+
$this->validator->validate($dto,$constraint);
1426+
1427+
$this->assertNoViolation();
1428+
}
14041429
}

‎src/Symfony/Bridge/Doctrine/Validator/Constraints/UniqueEntityValidator.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,12 @@ public function validate(mixed $value, Constraint $constraint): void
197197

198198
foreach ($constraint->identifierFieldNamesas$identifierFieldName) {
199199
$propertyValue =$this->getPropertyValue($entityClass,$identifierFieldName,current($result));
200+
if ($fieldValues[$identifierFieldName]instanceof \Stringable) {
201+
$fieldValues[$identifierFieldName] = (string)$fieldValues[$identifierFieldName];
202+
}
203+
if ($propertyValueinstanceof \Stringable) {
204+
$propertyValue = (string)$propertyValue;
205+
}
200206
if ($fieldValues[$identifierFieldName] !==$propertyValue) {
201207
$entityMatched =false;
202208
break;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp