Skip to content

Commit 8e1f62e

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

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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 UserUuidName
21+
{
22+
#[Id, Column]
23+
public ?Uuid $id = null;
24+
#[Column]
25+
public ?string $name = null;
26+
}

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,11 @@
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\UserUuidName;
4748
use Symfony\Bridge\Doctrine\Tests\TestRepositoryFactory;
4849
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
4950
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntityValidator;
51+
use Symfony\Component\Uid\Uuid;
5052
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
5153
use Symfony\Component\Validator\Exception\UnexpectedValueException;
5254
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
@@ -160,6 +162,7 @@ private function createSchema($em)
160162
$em->getClassMetadata(Employee::class),
161163
$em->getClassMetadata(CompositeObjectNoToStringIdEntity::class),
162164
$em->getClassMetadata(SingleIntIdStringWrapperNameEntity::class),
165+
$em->getClassMetadata(UserUuidName::class),
163166
]);
164167
}
165168

@@ -1489,4 +1492,42 @@ public function testEntityManagerNullObjectWhenDTODoctrineStyle()
14891492

14901493
$this->validator->validate($dto, $constraint);
14911494
}
1495+
1496+
public function testIdentifierFieldNamesObject()
1497+
{
1498+
$entity1 = new UserUuidName();
1499+
$entity1->id = Uuid::v7();
1500+
$entity1->name = 'Foo';
1501+
$entity2 = new UserUuidName();
1502+
$entity2->id = Uuid::v6();
1503+
$entity2->name = 'Foo';
1504+
1505+
$constraint = new UniqueEntity(
1506+
message: 'myMessage',
1507+
fields: ['name'],
1508+
entityClass: UserUuidName::class,
1509+
identifierFieldNames: ['id'],
1510+
em: self::EM_NAME,
1511+
);
1512+
$this->validator->validate($entity1, $constraint);
1513+
1514+
$this->assertNoViolation();
1515+
1516+
$this->em->persist($entity1);
1517+
$this->em->flush();
1518+
1519+
$this->validator->validate($entity1, $constraint);
1520+
1521+
$this->assertNoViolation();
1522+
1523+
$this->validator->validate($entity2, $constraint);
1524+
1525+
$this->buildViolation('myMessage')
1526+
->atPath('property.path.name')
1527+
->setParameter('{{ value }}', '"Foo"')
1528+
->setInvalidValue('Foo')
1529+
->setCause([$entity1])
1530+
->setCode(UniqueEntity::NOT_UNIQUE_ERROR)
1531+
->assertRaised();
1532+
}
14921533
}

0 commit comments

Comments
 (0)