@@ -50,46 +50,46 @@ public function process(mixed $data, Operation $operation, array $uriVariables =
50
50
// https://github.com/doctrine/orm/issues/8461#issuecomment-1250233555
51
51
if ($ operation instanceof HttpOperation && 'PUT ' === $ operation ->getMethod () && ($ operation ->getExtraProperties ()['standard_put ' ] ?? false )) {
52
52
\assert (method_exists ($ manager , 'getReference ' ));
53
- // TODO: the call to getReference is most likely to fail with complex identifiers
54
53
$ newData = $ data ;
55
- if ($ previousData = $ context ['previous_data ' ]) {
56
- $ newData = 1 === \count ($ uriVariables ) ? $ manager ->getReference ($ class , current ($ uriVariables )) : clone $ previousData ;
57
- }
58
-
59
54
$ identifiers = array_reverse ($ uriVariables );
60
55
$ links = $ this ->getLinks ($ class , $ operation , $ context );
61
56
$ reflectionProperties = $ this ->getReflectionProperties ($ data );
62
57
63
- if (!$ previousData ) {
64
- foreach (array_reverse ($ links ) as $ link ) {
65
- if ($ link ->getExpandedValue () || !$ link ->getFromClass ()) {
58
+ // TODO: the call to getReference is most likely to fail with complex identifiers
59
+ if ($ previousData = $ context ['previous_data ' ]) {
60
+ $ classMetadata = $ manager ->getClassMetadata ($ class );
61
+ $ identifiers = $ classMetadata ->getIdentifierValues ($ previousData );
62
+ $ newData = 1 === \count ($ identifiers ) ? $ manager ->getReference ($ class , current ($ identifiers )) : clone $ previousData ;
63
+
64
+ foreach ($ reflectionProperties as $ propertyName => $ reflectionProperty ) {
65
+ // // Don't override the property if it's part of the subresource system
66
+ if (isset ($ identifiers [$ propertyName ]) || isset ($ uriVariables [$ propertyName ])) {
66
67
continue ;
67
68
}
68
69
69
- $ identifierProperties = $ link ->getIdentifiers ();
70
- $ hasCompositeIdentifiers = 1 < \count ($ identifierProperties );
70
+ // Skip URI variables as sometime an uri variable is not the doctrine identifier
71
+ foreach ($ links as $ link ) {
72
+ if (\in_array ($ propertyName , $ link ->getIdentifiers (), true )) {
73
+ continue 2 ;
74
+ }
75
+ }
71
76
72
- foreach ($ identifierProperties as $ identifierProperty ) {
73
- $ reflectionProperty = $ reflectionProperties [$ identifierProperty ];
74
- $ reflectionProperty ->setValue ($ newData , $ this ->getIdentifierValue ($ identifiers , $ hasCompositeIdentifiers ? $ identifierProperty : null ));
77
+ if (($ newValue = $ reflectionProperty ->getValue ($ data )) !== $ reflectionProperty ->getValue ($ newData )) {
78
+ $ reflectionProperty ->setValue ($ newData , $ newValue );
75
79
}
76
80
}
77
81
} else {
78
- foreach ($ reflectionProperties as $ propertyName => $ reflectionProperty ) {
79
- // Don't override the property if it's part of the subresource system
80
- if (isset ($ uriVariables [$ propertyName ])) {
82
+ foreach (array_reverse ($ links ) as $ link ) {
83
+ if ($ link ->getExpandedValue () || !$ link ->getFromClass ()) {
81
84
continue ;
82
85
}
83
86
84
- foreach ($ links as $ link ) {
85
- $ identifierProperties = $ link ->getIdentifiers ();
86
- if (\in_array ($ propertyName , $ identifierProperties , true )) {
87
- continue ;
88
- }
87
+ $ identifierProperties = $ link ->getIdentifiers ();
88
+ $ hasCompositeIdentifiers = 1 < \count ($ identifierProperties );
89
89
90
- if (( $ newValue = $ reflectionProperty -> getValue ( $ data )) !== $ reflectionProperty -> getValue ( $ newData ) ) {
91
- $ reflectionProperty-> setValue ( $ newData , $ newValue ) ;
92
- }
90
+ foreach ( $ identifierProperties as $ identifierProperty ) {
91
+ $ reflectionProperty = $ reflectionProperties [ $ identifierProperty ] ;
92
+ $ reflectionProperty -> setValue ( $ newData , $ this -> getIdentifierValue ( $ identifiers , $ hasCompositeIdentifiers ? $ identifierProperty : null ));
93
93
}
94
94
}
95
95
}
@@ -113,7 +113,7 @@ public function process(mixed $data, Operation $operation, array $uriVariables =
113
113
private function isDeferredExplicit (DoctrineObjectManager $ manager , $ data ): bool
114
114
{
115
115
$ classMetadata = $ manager ->getClassMetadata ($ this ->getObjectClass ($ data ));
116
- if (method_exists ($ classMetadata , 'isChangeTrackingDeferredExplicit ' )) {
116
+ if ($ classMetadata && method_exists ($ classMetadata , 'isChangeTrackingDeferredExplicit ' )) { // @phpstan-ignore-line metadata can be null
117
117
return $ classMetadata ->isChangeTrackingDeferredExplicit ();
118
118
}
119
119
0 commit comments