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 264f7fc

Browse filesBrowse files
committed
forward-compatibility with field mappings in Doctrine ORM 4
1 parent 84ae858 commit 264f7fc
Copy full SHA for 264f7fc

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
@@ -13,6 +13,7 @@
1313

1414
use Doctrine\DBAL\Types\Types;
1515
use Doctrine\ORM\Mapping\ClassMetadataInfo;
16+
use Doctrine\ORM\Mapping\JoinColumnMapping;
1617
use Doctrine\ORM\Mapping\MappingException as LegacyMappingException;
1718
use Doctrine\Persistence\ManagerRegistry;
1819
use Doctrine\Persistence\Mapping\MappingException;
@@ -119,13 +120,13 @@ public function guessRequired(string $class, string $property)
119120
if ($classMetadata->isAssociationWithSingleJoinColumn($property)) {
120121
$mapping = $classMetadata->getAssociationMapping($property);
121122

122-
if (!isset($mapping['joinColumns'][0]['nullable'])) {
123+
if (null === self::getMappingValue($mapping['joinColumns'][0], 'nullable')) {
123124
// The "nullable" option defaults to true, in that case the
124125
// field should not be required.
125126
return new ValueGuess(false, Guess::HIGH_CONFIDENCE);
126127
}
127128

128-
return new ValueGuess(!$mapping['joinColumns'][0]['nullable'], Guess::HIGH_CONFIDENCE);
129+
return new ValueGuess(!self::getMappingValue($mapping['joinColumns'][0], 'nullable'), Guess::HIGH_CONFIDENCE);
129130
}
130131

131132
return null;
@@ -198,4 +199,13 @@ private static function getRealClass(string $class): string
198199

199200
return substr($class, $pos + Proxy::MARKER_LENGTH + 2);
200201
}
202+
203+
private static function getMappingValue(array|JoinColumnMapping $mapping, string $key): mixed
204+
{
205+
if ($mapping instanceof JoinColumnMapping) {
206+
return $mapping->$key;
207+
}
208+
209+
return $mapping[$key] ?? null;
210+
}
201211
}

‎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;
@@ -88,20 +91,20 @@ public function getTypes(string $class, string $property, array $context = [])
8891
if ($metadata instanceof ClassMetadata) {
8992
$associationMapping = $metadata->getAssociationMapping($property);
9093

91-
if (isset($associationMapping['indexBy'])) {
92-
$subMetadata = $this->entityManager->getClassMetadata($associationMapping['targetEntity']);
94+
if (self::getMappingValue($associationMapping, 'indexBy')) {
95+
$subMetadata = $this->entityManager->getClassMetadata(self::getMappingValue($associationMapping, 'targetEntity'));
9396

9497
// Check if indexBy value is a property
95-
$fieldName = $associationMapping['indexBy'];
98+
$fieldName = self::getMappingValue($associationMapping, 'indexBy');
9699
if (null === ($typeOfField = $subMetadata->getTypeOfField($fieldName))) {
97-
$fieldName = $subMetadata->getFieldForColumn($associationMapping['indexBy']);
100+
$fieldName = $subMetadata->getFieldForColumn(self::getMappingValue($associationMapping, 'indexBy'));
98101
// Not a property, maybe a column name?
99102
if (null === ($typeOfField = $subMetadata->getTypeOfField($fieldName))) {
100103
// Maybe the column name is the association join column?
101104
$associationMapping = $subMetadata->getAssociationMapping($fieldName);
102105

103106
$indexProperty = $subMetadata->getSingleAssociationReferencedJoinColumnName($fieldName);
104-
$subMetadata = $this->entityManager->getClassMetadata($associationMapping['targetEntity']);
107+
$subMetadata = $this->entityManager->getClassMetadata(self::getMappingValue($associationMapping, 'targetEntity'));
105108

106109
// Not a property, maybe a column name?
107110
if (null === ($typeOfField = $subMetadata->getTypeOfField($indexProperty))) {
@@ -128,7 +131,7 @@ public function getTypes(string $class, string $property, array $context = [])
128131
}
129132

130133
if ($metadata instanceof ClassMetadata && isset($metadata->embeddedClasses[$property])) {
131-
return [new Type(Type::BUILTIN_TYPE_OBJECT, false, $metadata->embeddedClasses[$property]['class'])];
134+
return [new Type(Type::BUILTIN_TYPE_OBJECT, false, self::getMappingValue($metadata->embeddedClasses[$property], 'class'))];
132135
}
133136

134137
if ($metadata->hasField($property)) {
@@ -140,7 +143,7 @@ public function getTypes(string $class, string $property, array $context = [])
140143

141144
$nullable = $metadata instanceof ClassMetadata && $metadata->isNullable($property);
142145
$enumType = null;
143-
if (null !== $enumClass = $metadata->getFieldMapping($property)['enumType'] ?? null) {
146+
if (null !== $enumClass = self::getMappingValue($metadata->getFieldMapping($property), 'enumType') ?? null) {
144147
$enumType = new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, $enumClass);
145148
}
146149

@@ -236,17 +239,17 @@ private function getMetadata(string $class): ?ClassMetadata
236239
*/
237240
private function isAssociationNullable($associationMapping): bool
238241
{
239-
if (isset($associationMapping['id']) && $associationMapping['id']) {
242+
if (self::getMappingValue($associationMapping, 'id')) {
240243
return false;
241244
}
242245

243-
if (!isset($associationMapping['joinColumns'])) {
246+
if (!self::getMappingValue($associationMapping, 'joinColumns')) {
244247
return true;
245248
}
246249

247-
$joinColumns = $associationMapping['joinColumns'];
250+
$joinColumns = self::getMappingValue($associationMapping, 'joinColumns');
248251
foreach ($joinColumns as $joinColumn) {
249-
if (isset($joinColumn['nullable']) && !$joinColumn['nullable']) {
252+
if (false === self::getMappingValue($joinColumn, 'nullable')) {
250253
return false;
251254
}
252255
}
@@ -302,4 +305,13 @@ private function getPhpType(string $doctrineType): ?string
302305

303306
return null;
304307
}
308+
309+
private static function getMappingValue(array|AssociationMapping|EmbeddedClassMapping|FieldMapping|JoinColumnMapping $mapping, string $key): mixed
310+
{
311+
if ($mapping instanceof AssociationMapping || $mapping instanceof EmbeddedClassMapping || $mapping instanceof FieldMapping || $mapping instanceof JoinColumnMapping) {
312+
return $mapping->$key;
313+
}
314+
315+
return $mapping[$key] ?? null;
316+
}
305317
}

‎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;
@@ -75,7 +76,7 @@ public function loadClassMetadata(ClassMetadata $metadata): bool
7576
foreach ($doctrineMetadata->fieldMappings as $mapping) {
7677
$enabledForProperty = $enabledForClass;
7778
$lengthConstraint = null;
78-
foreach ($metadata->getPropertyMetadata($mapping['fieldName']) as $propertyMetadata) {
79+
foreach ($metadata->getPropertyMetadata(self::getFieldMappingValue($mapping, 'fieldName')) as $propertyMetadata) {
7980
// Enabling or disabling auto-mapping explicitly always takes precedence
8081
if (AutoMappingStrategy::DISABLED === $propertyMetadata->getAutoMappingStrategy()) {
8182
continue 2;
@@ -95,26 +96,26 @@ public function loadClassMetadata(ClassMetadata $metadata): bool
9596
continue;
9697
}
9798

98-
if (true === ($mapping['unique'] ?? false) && !isset($existingUniqueFields[$mapping['fieldName']])) {
99-
$metadata->addConstraint(new UniqueEntity(['fields' => $mapping['fieldName']]));
99+
if (true === (self::getFieldMappingValue($mapping, 'unique') ?? false) && !isset($existingUniqueFields[self::getFieldMappingValue($mapping, 'fieldName')])) {
100+
$metadata->addConstraint(new UniqueEntity(['fields' => self::getFieldMappingValue($mapping, 'fieldName')]));
100101
$loaded = true;
101102
}
102103

103-
if (null === ($mapping['length'] ?? null) || null !== ($mapping['enumType'] ?? null) || !\in_array($mapping['type'], ['string', 'text'], true)) {
104+
if (null === (self::getFieldMappingValue($mapping, 'length') ?? null) || null !== (self::getFieldMappingValue($mapping, 'enumType') ?? null) || !\in_array(self::getFieldMappingValue($mapping, 'type'), ['string', 'text'], true)) {
104105
continue;
105106
}
106107

107108
if (null === $lengthConstraint) {
108-
if (isset($mapping['originalClass']) && !str_contains($mapping['declaredField'], '.')) {
109-
$metadata->addPropertyConstraint($mapping['declaredField'], new Valid());
109+
if (self::getFieldMappingValue($mapping, 'originalClass') && !str_contains(self::getFieldMappingValue($mapping, 'declaredField'), '.')) {
110+
$metadata->addPropertyConstraint(self::getFieldMappingValue($mapping, 'declaredField'), new Valid());
110111
$loaded = true;
111-
} elseif (property_exists($className, $mapping['fieldName']) && (!$doctrineMetadata->isMappedSuperclass || $metadata->getReflectionClass()->getProperty($mapping['fieldName'])->isPrivate())) {
112-
$metadata->addPropertyConstraint($mapping['fieldName'], new Length(['max' => $mapping['length']]));
112+
} elseif (property_exists($className, self::getFieldMappingValue($mapping, 'fieldName')) && (!$doctrineMetadata->isMappedSuperclass || $metadata->getReflectionClass()->getProperty(self::getFieldMappingValue($mapping, 'fieldName'))->isPrivate())) {
113+
$metadata->addPropertyConstraint(self::getFieldMappingValue($mapping, 'fieldName'), new Length(['max' => self::getFieldMappingValue($mapping, 'length')]));
113114
$loaded = true;
114115
}
115116
} elseif (null === $lengthConstraint->max) {
116117
// If a Length constraint exists and no max length has been explicitly defined, set it
117-
$lengthConstraint->max = $mapping['length'];
118+
$lengthConstraint->max = self::getFieldMappingValue($mapping, 'length');
118119
}
119120
}
120121

@@ -138,4 +139,13 @@ private function getExistingUniqueFields(ClassMetadata $metadata): array
138139

139140
return $fields;
140141
}
142+
143+
private static function getFieldMappingValue(array|FieldMapping $mapping, string $key): mixed
144+
{
145+
if ($mapping instanceof FieldMapping) {
146+
return $mapping->$key;
147+
}
148+
149+
return $mapping[$key] ?? null;
150+
}
141151
}

0 commit comments

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