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

Appearance settings

Commit 94f00bd

Browse filesBrowse files
committed
forward-compatibility with field mappings in Doctrine ORM 4
1 parent 03ebef7 commit 94f00bd
Copy full SHA for 94f00bd

File tree

3 files changed

+54
-22
lines changed
Filter options

3 files changed

+54
-22
lines changed

‎src/Symfony/Bridge/Doctrine/Form/DoctrineOrmTypeGuesser.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Doctrine/Form/DoctrineOrmTypeGuesser.php
+12-2Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Doctrine\DBAL\Types\Types;
1515
use Doctrine\ORM\Mapping\ClassMetadata;
1616
use Doctrine\ORM\Mapping\ClassMetadataInfo;
17+
use Doctrine\ORM\Mapping\JoinColumnMapping;
1718
use Doctrine\ORM\Mapping\MappingException as LegacyMappingException;
1819
use Doctrine\Persistence\ManagerRegistry;
1920
use Doctrine\Persistence\Mapping\MappingException;
@@ -110,13 +111,13 @@ public function guessRequired(string $class, string $property): ?ValueGuess
110111
if ($classMetadata->isAssociationWithSingleJoinColumn($property)) {
111112
$mapping = $classMetadata->getAssociationMapping($property);
112113

113-
if (!isset($mapping['joinColumns'][0]['nullable'])) {
114+
if (null === self::getMappingValue($mapping['joinColumns'][0], 'nullable')) {
114115
// The "nullable" option defaults to true, in that case the
115116
// field should not be required.
116117
return new ValueGuess(false, Guess::HIGH_CONFIDENCE);
117118
}
118119

119-
return new ValueGuess(!$mapping['joinColumns'][0]['nullable'], Guess::HIGH_CONFIDENCE);
120+
return new ValueGuess(!self::getMappingValue($mapping['joinColumns'][0], 'nullable'), Guess::HIGH_CONFIDENCE);
120121
}
121122

122123
return null;
@@ -190,4 +191,13 @@ private static function getRealClass(string $class): string
190191

191192
return substr($class, $pos + Proxy::MARKER_LENGTH + 2);
192193
}
194+
195+
private static function getMappingValue(array|JoinColumnMapping $mapping, string $key): mixed
196+
{
197+
if ($mapping instanceof JoinColumnMapping) {
198+
return $mapping->$key;
199+
}
200+
201+
return $mapping[$key] ?? null;
202+
}
193203
}

‎src/Symfony/Bridge/Doctrine/PropertyInfo/DoctrineExtractor.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Doctrine/PropertyInfo/DoctrineExtractor.php
+23-11Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
use Doctrine\ORM\EntityManagerInterface;
1717
use Doctrine\ORM\Mapping\AssociationMapping;
1818
use Doctrine\ORM\Mapping\ClassMetadata;
19+
use Doctrine\ORM\Mapping\EmbeddedClassMapping;
20+
use Doctrine\ORM\Mapping\FieldMapping;
21+
use Doctrine\ORM\Mapping\JoinColumnMapping;
1922
use Doctrine\ORM\Mapping\MappingException as OrmMappingException;
2023
use Doctrine\Persistence\Mapping\MappingException;
2124
use Symfony\Component\PropertyInfo\PropertyAccessExtractorInterface;
@@ -78,20 +81,20 @@ public function getTypes(string $class, string $property, array $context = []):
7881
if ($metadata instanceof ClassMetadata) {
7982
$associationMapping = $metadata->getAssociationMapping($property);
8083

81-
if (isset($associationMapping['indexBy'])) {
82-
$subMetadata = $this->entityManager->getClassMetadata($associationMapping['targetEntity']);
84+
if (self::getMappingValue($associationMapping, 'indexBy')) {
85+
$subMetadata = $this->entityManager->getClassMetadata(self::getMappingValue($associationMapping, 'targetEntity'));
8386

8487
// Check if indexBy value is a property
85-
$fieldName = $associationMapping['indexBy'];
88+
$fieldName = self::getMappingValue($associationMapping, 'indexBy');
8689
if (null === ($typeOfField = $subMetadata->getTypeOfField($fieldName))) {
87-
$fieldName = $subMetadata->getFieldForColumn($associationMapping['indexBy']);
90+
$fieldName = $subMetadata->getFieldForColumn(self::getMappingValue($associationMapping, 'indexBy'));
8891
// Not a property, maybe a column name?
8992
if (null === ($typeOfField = $subMetadata->getTypeOfField($fieldName))) {
9093
// Maybe the column name is the association join column?
9194
$associationMapping = $subMetadata->getAssociationMapping($fieldName);
9295

9396
$indexProperty = $subMetadata->getSingleAssociationReferencedJoinColumnName($fieldName);
94-
$subMetadata = $this->entityManager->getClassMetadata($associationMapping['targetEntity']);
97+
$subMetadata = $this->entityManager->getClassMetadata(self::getMappingValue($associationMapping, 'targetEntity'));
9598

9699
// Not a property, maybe a column name?
97100
if (null === ($typeOfField = $subMetadata->getTypeOfField($indexProperty))) {
@@ -118,7 +121,7 @@ public function getTypes(string $class, string $property, array $context = []):
118121
}
119122

120123
if ($metadata instanceof ClassMetadata && isset($metadata->embeddedClasses[$property])) {
121-
return [new Type(Type::BUILTIN_TYPE_OBJECT, false, $metadata->embeddedClasses[$property]['class'])];
124+
return [new Type(Type::BUILTIN_TYPE_OBJECT, false, self::getMappingValue($metadata->embeddedClasses[$property], 'class'))];
122125
}
123126

124127
if ($metadata->hasField($property)) {
@@ -130,7 +133,7 @@ public function getTypes(string $class, string $property, array $context = []):
130133

131134
$nullable = $metadata instanceof ClassMetadata && $metadata->isNullable($property);
132135
$enumType = null;
133-
if (null !== $enumClass = $metadata->getFieldMapping($property)['enumType'] ?? null) {
136+
if (null !== $enumClass = self::getMappingValue($metadata->getFieldMapping($property), 'enumType') ?? null) {
134137
$enumType = new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, $enumClass);
135138
}
136139

@@ -220,17 +223,17 @@ private function getMetadata(string $class): ?ClassMetadata
220223
*/
221224
private function isAssociationNullable(array|AssociationMapping $associationMapping): bool
222225
{
223-
if (isset($associationMapping['id']) && $associationMapping['id']) {
226+
if (self::getMappingValue($associationMapping, 'id')) {
224227
return false;
225228
}
226229

227-
if (!isset($associationMapping['joinColumns'])) {
230+
if (!self::getMappingValue($associationMapping, 'joinColumns')) {
228231
return true;
229232
}
230233

231-
$joinColumns = $associationMapping['joinColumns'];
234+
$joinColumns = self::getMappingValue($associationMapping, 'joinColumns');
232235
foreach ($joinColumns as $joinColumn) {
233-
if (isset($joinColumn['nullable']) && !$joinColumn['nullable']) {
236+
if (false === self::getMappingValue($joinColumn, 'nullable')) {
234237
return false;
235238
}
236239
}
@@ -272,4 +275,13 @@ private function getPhpType(string $doctrineType): ?string
272275
default => null,
273276
};
274277
}
278+
279+
private static function getMappingValue(array|AssociationMapping|EmbeddedClassMapping|FieldMapping|JoinColumnMapping $mapping, string $key): mixed
280+
{
281+
if ($mapping instanceof AssociationMapping || $mapping instanceof EmbeddedClassMapping || $mapping instanceof FieldMapping || $mapping instanceof JoinColumnMapping) {
282+
return $mapping->$key;
283+
}
284+
285+
return $mapping[$key] ?? null;
286+
}
275287
}

‎src/Symfony/Bridge/Doctrine/Validator/DoctrineLoader.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Doctrine/Validator/DoctrineLoader.php
+19-9Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Doctrine\ORM\EntityManagerInterface;
1515
use Doctrine\ORM\Mapping\ClassMetadata as OrmClassMetadata;
16+
use Doctrine\ORM\Mapping\FieldMapping;
1617
use Doctrine\ORM\Mapping\MappingException as OrmMappingException;
1718
use Doctrine\Persistence\Mapping\MappingException;
1819
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
@@ -69,7 +70,7 @@ public function loadClassMetadata(ClassMetadata $metadata): bool
6970
foreach ($doctrineMetadata->fieldMappings as $mapping) {
7071
$enabledForProperty = $enabledForClass;
7172
$lengthConstraint = null;
72-
foreach ($metadata->getPropertyMetadata($mapping['fieldName']) as $propertyMetadata) {
73+
foreach ($metadata->getPropertyMetadata(self::getFieldMappingValue($mapping, 'fieldName')) as $propertyMetadata) {
7374
// Enabling or disabling auto-mapping explicitly always takes precedence
7475
if (AutoMappingStrategy::DISABLED === $propertyMetadata->getAutoMappingStrategy()) {
7576
continue 2;
@@ -89,26 +90,26 @@ public function loadClassMetadata(ClassMetadata $metadata): bool
8990
continue;
9091
}
9192

92-
if (true === ($mapping['unique'] ?? false) && !isset($existingUniqueFields[$mapping['fieldName']])) {
93-
$metadata->addConstraint(new UniqueEntity(['fields' => $mapping['fieldName']]));
93+
if (true === (self::getFieldMappingValue($mapping, 'unique') ?? false) && !isset($existingUniqueFields[self::getFieldMappingValue($mapping, 'fieldName')])) {
94+
$metadata->addConstraint(new UniqueEntity(['fields' => self::getFieldMappingValue($mapping, 'fieldName')]));
9495
$loaded = true;
9596
}
9697

97-
if (null === ($mapping['length'] ?? null) || null !== ($mapping['enumType'] ?? null) || !\in_array($mapping['type'], ['string', 'text'], true)) {
98+
if (null === (self::getFieldMappingValue($mapping, 'length') ?? null) || null !== (self::getFieldMappingValue($mapping, 'enumType') ?? null) || !\in_array(self::getFieldMappingValue($mapping, 'type'), ['string', 'text'], true)) {
9899
continue;
99100
}
100101

101102
if (null === $lengthConstraint) {
102-
if (isset($mapping['originalClass']) && !str_contains($mapping['declaredField'], '.')) {
103-
$metadata->addPropertyConstraint($mapping['declaredField'], new Valid());
103+
if (self::getFieldMappingValue($mapping, 'originalClass') && !str_contains(self::getFieldMappingValue($mapping, 'declaredField'), '.')) {
104+
$metadata->addPropertyConstraint(self::getFieldMappingValue($mapping, 'declaredField'), new Valid());
104105
$loaded = true;
105-
} elseif (property_exists($className, $mapping['fieldName']) && (!$doctrineMetadata->isMappedSuperclass || $metadata->getReflectionClass()->getProperty($mapping['fieldName'])->isPrivate())) {
106-
$metadata->addPropertyConstraint($mapping['fieldName'], new Length(['max' => $mapping['length']]));
106+
} elseif (property_exists($className, self::getFieldMappingValue($mapping, 'fieldName')) && (!$doctrineMetadata->isMappedSuperclass || $metadata->getReflectionClass()->getProperty(self::getFieldMappingValue($mapping, 'fieldName'))->isPrivate())) {
107+
$metadata->addPropertyConstraint(self::getFieldMappingValue($mapping, 'fieldName'), new Length(['max' => self::getFieldMappingValue($mapping, 'length')]));
107108
$loaded = true;
108109
}
109110
} elseif (null === $lengthConstraint->max) {
110111
// If a Length constraint exists and no max length has been explicitly defined, set it
111-
$lengthConstraint->max = $mapping['length'];
112+
$lengthConstraint->max = self::getFieldMappingValue($mapping, 'length');
112113
}
113114
}
114115

@@ -132,4 +133,13 @@ private function getExistingUniqueFields(ClassMetadata $metadata): array
132133

133134
return $fields;
134135
}
136+
137+
private static function getFieldMappingValue(array|FieldMapping $mapping, string $key): mixed
138+
{
139+
if ($mapping instanceof FieldMapping) {
140+
return $mapping->$key;
141+
}
142+
143+
return $mapping[$key] ?? null;
144+
}
135145
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.