@@ -60,9 +60,9 @@ public function resolve(Request $request, ArgumentMetadata $argument): array
60
60
$ message = sprintf (' The expression "%s" returned null. ' , $ options ->expr );
61
61
}
62
62
// find by identifier?
63
- } elseif (false === $ object = $ this ->find ($ manager , $ request , $ options , $ argument-> getName () )) {
63
+ } elseif (false === $ object = $ this ->find ($ manager , $ request , $ options , $ argument )) {
64
64
// find by criteria
65
- if (!$ criteria = $ this ->getCriteria ($ request , $ options , $ manager )) {
65
+ if (!$ criteria = $ this ->getCriteria ($ request , $ options , $ manager, $ argument )) {
66
66
return [];
67
67
}
68
68
try {
@@ -94,13 +94,13 @@ private function getManager(?string $name, string $class): ?ObjectManager
94
94
return $ manager ->getMetadataFactory ()->isTransient ($ class ) ? null : $ manager ;
95
95
}
96
96
97
- private function find (ObjectManager $ manager , Request $ request , MapEntity $ options , string $ name ): false |object |null
97
+ private function find (ObjectManager $ manager , Request $ request , MapEntity $ options , ArgumentMetadata $ argument ): false |object |null
98
98
{
99
99
if ($ options ->mapping || $ options ->exclude ) {
100
100
return false ;
101
101
}
102
102
103
- $ id = $ this ->getIdentifier ($ request , $ options , $ name );
103
+ $ id = $ this ->getIdentifier ($ request , $ options , $ argument );
104
104
if (false === $ id || null === $ id ) {
105
105
return $ id ;
106
106
}
@@ -119,14 +119,14 @@ private function find(ObjectManager $manager, Request $request, MapEntity $optio
119
119
}
120
120
}
121
121
122
- private function getIdentifier (Request $ request , MapEntity $ options , string $ name ): mixed
122
+ private function getIdentifier (Request $ request , MapEntity $ options , ArgumentMetadata $ argument ): mixed
123
123
{
124
124
if (\is_array ($ options ->id )) {
125
125
$ id = [];
126
126
foreach ($ options ->id as $ field ) {
127
127
// Convert "%s_uuid" to "foobar_uuid"
128
128
if (str_contains ($ field , '%s ' )) {
129
- $ field = sprintf ($ field , $ name );
129
+ $ field = sprintf ($ field , $ argument -> getName () );
130
130
}
131
131
132
132
$ id [$ field ] = $ request ->attributes ->get ($ field );
@@ -135,28 +135,54 @@ private function getIdentifier(Request $request, MapEntity $options, string $nam
135
135
return $ id ;
136
136
}
137
137
138
- if (null !== $ options ->id ) {
139
- $ name = $ options ->id ;
138
+ if ($ options ->id ) {
139
+ return $ request -> attributes -> get ( $ options -> id ) ?? ( $ options ->stripNull ? false : null ) ;
140
140
}
141
141
142
+ $ name = $ argument ->getName ();
143
+
142
144
if ($ request ->attributes ->has ($ name )) {
143
- return $ request ->attributes ->get ($ name ) ?? ($ options ->stripNull ? false : null );
145
+ if (\is_array ($ id = $ request ->attributes ->get ($ name ))) {
146
+ return false ;
147
+ }
148
+
149
+ foreach ($ request ->attributes ->get ('_route_mapping ' ) ?? [] as $ parameter => $ attribute ) {
150
+ if ($ name === $ attribute ) {
151
+ $ options ->mapping = [$ name => $ parameter ];
152
+
153
+ return false ;
154
+ }
155
+ }
156
+
157
+ return $ id ?? ($ options ->stripNull ? false : null );
144
158
}
159
+ if ($ request ->attributes ->has ('id ' )) {
160
+ trigger_deprecation ('symfony/doctrine-bridge ' , '7.2 ' , 'Relying on auto-mapping for Doctrine entities is deprecated for argument $%s of "%s": declare the mapping using either the #[MapEntity] attribute or mapped route parameters. ' , $ argument ->getName (), $ argument ->getControllerName ());
145
161
146
- if (!$ options ->id && $ request ->attributes ->has ('id ' )) {
147
162
return $ request ->attributes ->get ('id ' ) ?? ($ options ->stripNull ? false : null );
148
163
}
149
164
150
165
return false ;
151
166
}
152
167
153
- private function getCriteria (Request $ request , MapEntity $ options , ObjectManager $ manager ): array
168
+ private function getCriteria (Request $ request , MapEntity $ options , ObjectManager $ manager, ArgumentMetadata $ argument ): array
154
169
{
155
- if (null === $ mapping = $ options ->mapping ) {
170
+ if (!($ mapping = $ options ->mapping ) && \is_array ($ criteria = $ request ->attributes ->get ($ argument ->getName ()))) {
171
+ foreach ($ options ->exclude as $ exclude ) {
172
+ unset($ criteria [$ exclude ]);
173
+ }
174
+
175
+ if ($ options ->stripNull ) {
176
+ $ criteria = array_filter ($ criteria , static fn ($ value ) => null !== $ value );
177
+ }
178
+
179
+ return $ criteria ;
180
+ } elseif (null === $ mapping ) {
181
+ trigger_deprecation ('symfony/doctrine-bridge ' , '7.2 ' , 'Relying on auto-mapping for Doctrine entities is deprecated for argument $%s of "%s": declare the identifier using either the #[MapEntity] attribute or mapped route parameters. ' , $ argument ->getName (), $ argument ->getControllerName ());
156
182
$ mapping = $ request ->attributes ->keys ();
157
183
}
158
184
159
- if ($ mapping && \is_array ( $ mapping ) && array_is_list ($ mapping )) {
185
+ if ($ mapping && array_is_list ($ mapping )) {
160
186
$ mapping = array_combine ($ mapping , $ mapping );
161
187
}
162
188
@@ -168,17 +194,11 @@ private function getCriteria(Request $request, MapEntity $options, ObjectManager
168
194
return [];
169
195
}
170
196
171
- // if a specific id has been defined in the options and there is no corresponding attribute
172
- // return false in order to avoid a fallback to the id which might be of another object
173
- if (\is_string ($ options ->id ) && null === $ request ->attributes ->get ($ options ->id )) {
174
- return [];
175
- }
176
-
177
197
$ criteria = [];
178
- $ metadata = $ manager ->getClassMetadata ($ options ->class );
198
+ $ metadata = null === $ options -> mapping ? $ manager ->getClassMetadata ($ options ->class ) : false ;
179
199
180
200
foreach ($ mapping as $ attribute => $ field ) {
181
- if (!$ metadata ->hasField ($ field ) && (!$ metadata ->hasAssociation ($ field ) || !$ metadata ->isSingleValuedAssociation ($ field ))) {
201
+ if ($ metadata && !$ metadata ->hasField ($ field ) && (!$ metadata ->hasAssociation ($ field ) || !$ metadata ->isSingleValuedAssociation ($ field ))) {
182
202
continue ;
183
203
}
184
204
0 commit comments