Skip to content

Commit 898174b

Browse files
[DoctrineBridge] Fix UniqueEntity for non-integer identifiers
1 parent 13de120 commit 898174b

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-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+
namespace Symfony\Bridge\Doctrine\Tests\Fixtures;
13+
14+
use Symfony\Component\Uid\Uuid;
15+
16+
class UserUuidNameDto
17+
{
18+
public function __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+
namespace Symfony\Bridge\Doctrine\Tests\Fixtures;
13+
14+
use Doctrine\ORM\Mapping\Column;
15+
use Doctrine\ORM\Mapping\Entity;
16+
use Doctrine\ORM\Mapping\Id;
17+
use Symfony\Component\Uid\Uuid;
18+
19+
#[Entity]
20+
class UserUuidNameEntity
21+
{
22+
public function __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: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,12 @@
4444
use Symfony\Bridge\Doctrine\Tests\Fixtures\UpdateCompositeIntIdEntity;
4545
use Symfony\Bridge\Doctrine\Tests\Fixtures\UpdateCompositeObjectNoToStringIdEntity;
4646
use Symfony\Bridge\Doctrine\Tests\Fixtures\UpdateEmployeeProfile;
47+
use Symfony\Bridge\Doctrine\Tests\Fixtures\UserUuidNameDto;
48+
use Symfony\Bridge\Doctrine\Tests\Fixtures\UserUuidNameEntity;
4749
use Symfony\Bridge\Doctrine\Tests\TestRepositoryFactory;
4850
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
4951
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntityValidator;
52+
use Symfony\Component\Uid\Uuid;
5053
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
5154
use Symfony\Component\Validator\Exception\UnexpectedValueException;
5255
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
@@ -160,6 +163,7 @@ private function createSchema($em)
160163
$em->getClassMetadata(Employee::class),
161164
$em->getClassMetadata(CompositeObjectNoToStringIdEntity::class),
162165
$em->getClassMetadata(SingleIntIdStringWrapperNameEntity::class),
166+
$em->getClassMetadata(UserUuidNameEntity::class),
163167
]);
164168
}
165169

@@ -1489,4 +1493,26 @@ public function testEntityManagerNullObjectWhenDTODoctrineStyle()
14891493

14901494
$this->validator->validate($dto, $constraint);
14911495
}
1496+
1497+
public function testUuidIdentifierWithSameValueDifferentInstanceDoesNotCauseViolation()
1498+
{
1499+
$uuidString = 'ec562e21-1fc8-4e55-8de7-a42389ac75c5';
1500+
$existingPerson = new UserUuidNameEntity(Uuid::fromString($uuidString), 'Finnley Benton');
1501+
$this->em->persist($existingPerson);
1502+
$this->em->flush();
1503+
1504+
$dto = new UserUuidNameDto(Uuid::fromString($uuidString), 'Finnley Benton', '');
1505+
1506+
$constraint = new UniqueEntity(
1507+
fields: ['fullName'],
1508+
entityClass: UserUuidNameEntity::class,
1509+
identifierFieldNames: ['id'],
1510+
em: self::EM_NAME,
1511+
message: 'myMessage'
1512+
);
1513+
1514+
$this->validator->validate($dto, $constraint);
1515+
1516+
$this->assertNoViolation();
1517+
}
14921518
}

0 commit comments

Comments
 (0)