13
13
14
14
use Doctrine \ORM \EntityManagerInterface ;
15
15
use Doctrine \ORM \Mapping \ClassMetadata as OrmClassMetadata ;
16
+ use Doctrine \ORM \Mapping \FieldMapping ;
16
17
use Doctrine \ORM \Mapping \MappingException as OrmMappingException ;
17
18
use Doctrine \Persistence \Mapping \MappingException ;
18
19
use Symfony \Bridge \Doctrine \Validator \Constraints \UniqueEntity ;
@@ -69,7 +70,7 @@ public function loadClassMetadata(ClassMetadata $metadata): bool
69
70
foreach ($ doctrineMetadata ->fieldMappings as $ mapping ) {
70
71
$ enabledForProperty = $ enabledForClass ;
71
72
$ lengthConstraint = null ;
72
- foreach ($ metadata ->getPropertyMetadata ($ mapping[ 'fieldName ' ] ) as $ propertyMetadata ) {
73
+ foreach ($ metadata ->getPropertyMetadata (self :: getFieldMappingValue ( $ mapping, 'fieldName ' ) ) as $ propertyMetadata ) {
73
74
// Enabling or disabling auto-mapping explicitly always takes precedence
74
75
if (AutoMappingStrategy::DISABLED === $ propertyMetadata ->getAutoMappingStrategy ()) {
75
76
continue 2 ;
@@ -89,26 +90,26 @@ public function loadClassMetadata(ClassMetadata $metadata): bool
89
90
continue ;
90
91
}
91
92
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 ' ) ]));
94
95
$ loaded = true ;
95
96
}
96
97
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 )) {
98
99
continue ;
99
100
}
100
101
101
102
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 ());
104
105
$ 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 ' ) ]));
107
108
$ loaded = true ;
108
109
}
109
110
} elseif (null === $ lengthConstraint ->max ) {
110
111
// 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 ' ) ;
112
113
}
113
114
}
114
115
@@ -132,4 +133,13 @@ private function getExistingUniqueFields(ClassMetadata $metadata): array
132
133
133
134
return $ fields ;
134
135
}
136
+
137
+ private static function getFieldMappingValue (FieldMapping |array $ mapping , string $ key ): mixed
138
+ {
139
+ if ($ mapping instanceof FieldMapping) {
140
+ return $ mapping ->$ key ;
141
+ }
142
+
143
+ return $ mapping [$ key ] ?? null ;
144
+ }
135
145
}
0 commit comments