Skip to content

Commit 7205dbc

Browse files
committed
fix compatibility with Doctrine ORM 4
1 parent 22df1e8 commit 7205dbc

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

Tests/Validator/Constraints/UniqueEntityValidatorTest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Doctrine\ORM\EntityRepository;
1717
use Doctrine\ORM\Mapping\ClassMetadata;
1818
use Doctrine\ORM\Mapping\ClassMetadataInfo;
19+
use Doctrine\ORM\Mapping\PropertyAccessors\RawValuePropertyAccessor;
1920
use Doctrine\ORM\Tools\SchemaTool;
2021
use Doctrine\Persistence\ManagerRegistry;
2122
use Doctrine\Persistence\ObjectManager;
@@ -115,11 +116,21 @@ class_exists(ClassMetadataInfo::class) ? ClassMetadataInfo::class : ClassMetadat
115116
->willReturn(true)
116117
;
117118
$refl = $this->createMock(\ReflectionProperty::class);
119+
$refl
120+
->method('getName')
121+
->willReturn('name')
122+
;
118123
$refl
119124
->method('getValue')
120125
->willReturn(true)
121126
;
122-
$classMetadata->reflFields = ['name' => $refl];
127+
128+
if (property_exists(ClassMetadata::class, 'propertyAccessors')) {
129+
$classMetadata->propertyAccessors['name'] = RawValuePropertyAccessor::fromReflectionProperty($refl);
130+
} else {
131+
$classMetadata->reflFields = ['name' => $refl];
132+
}
133+
123134
$em->expects($this->any())
124135
->method('getClassMetadata')
125136
->willReturn($classMetadata)

Validator/Constraints/UniqueEntityValidator.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Bridge\Doctrine\Validator\Constraints;
1313

14+
use Doctrine\ORM\Mapping\ClassMetadata as OrmClassMetadata;
1415
use Doctrine\Persistence\ManagerRegistry;
1516
use Doctrine\Persistence\Mapping\ClassMetadata;
1617
use Doctrine\Persistence\ObjectManager;
@@ -92,7 +93,11 @@ public function validate(mixed $entity, Constraint $constraint)
9293
throw new ConstraintDefinitionException(sprintf('The field "%s" is not mapped by Doctrine, so it cannot be validated for uniqueness.', $fieldName));
9394
}
9495

95-
$fieldValue = $class->reflFields[$fieldName]->getValue($entity);
96+
if (property_exists(OrmClassMetadata::class, 'propertyAccessors')) {
97+
$fieldValue = $class->propertyAccessors[$fieldName]->getValue($entity);
98+
} else {
99+
$fieldValue = $class->reflFields[$fieldName]->getValue($entity);
100+
}
96101

97102
if (null === $fieldValue && $this->ignoreNullForField($constraint, $fieldName)) {
98103
$hasIgnorableNullValue = true;

0 commit comments

Comments
 (0)