Skip to content

Commit 7ab1fb1

Browse files
committed
UniqueEntityValidator: Retrieve at max two entities for unique entity check.
Fixes: #43254
1 parent 1168951 commit 7ab1fb1

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* Add `DoctrineOpenTransactionLoggerMiddleware` to log when a transaction has been left open
88
* Deprecate `PdoCacheAdapterDoctrineSchemaSubscriber` and add `DoctrineDbalCacheAdapterSchemaSubscriber` instead
9+
* `UniqueEntity` constraint retrieves a maximum of two entities if the default repository method is used.
910

1011
5.3
1112
---

Validator/Constraints/UniqueEntityValidator.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,18 @@ public function validate($entity, Constraint $constraint)
134134
$repository = $em->getRepository(\get_class($entity));
135135
}
136136

137-
$result = $repository->{$constraint->repositoryMethod}($criteria);
137+
$arguments = [$criteria];
138+
139+
/* If the default repository method is used, it is always enough to retrieve at most two entities because:
140+
* - No entity returned, the current entity is definitely unique.
141+
* - More than one entity returned, the current entity cannot be unique.
142+
* - One entity returned the uniqueness depends on the current entity.
143+
*/
144+
if ('findBy' === $constraint->repositoryMethod) {
145+
$arguments = [$criteria, null, 2];
146+
}
147+
148+
$result = $repository->{$constraint->repositoryMethod}(...$arguments);
138149

139150
if ($result instanceof \IteratorAggregate) {
140151
$result = $result->getIterator();

0 commit comments

Comments
 (0)