21
21
use Symfony \Component \HttpFoundation \Request ;
22
22
use Symfony \Component \HttpKernel \Controller \ValueResolverInterface ;
23
23
use Symfony \Component \HttpKernel \ControllerMetadata \ArgumentMetadata ;
24
+ use Symfony \Component \HttpKernel \Exception \NearMissValueResolverException ;
24
25
use Symfony \Component \HttpKernel \Exception \NotFoundHttpException ;
25
26
26
27
/**
@@ -123,15 +124,21 @@ private function getIdentifier(Request $request, MapEntity $options, string $nam
123
124
{
124
125
if (\is_array ($ options ->id )) {
125
126
$ id = [];
127
+ $ usedAttributes = [];
126
128
foreach ($ options ->id as $ field ) {
127
129
// Convert "%s_uuid" to "foobar_uuid"
128
130
if (str_contains ($ field , '%s ' )) {
129
131
$ field = sprintf ($ field , $ name );
130
132
}
131
133
134
+ $ usedAttributes [] = $ field ;
132
135
$ id [$ field ] = $ request ->attributes ->get ($ field );
133
136
}
134
137
138
+ if ($ usedAttributes ) {
139
+ $ request ->attributes ->set ('_entity_used_attributes ' , array_merge ($ request ->attributes ->get ('_entity_used_attributes ' ) ?? [], $ usedAttributes ));
140
+ }
141
+
135
142
return $ id ;
136
143
}
137
144
@@ -140,10 +147,14 @@ private function getIdentifier(Request $request, MapEntity $options, string $nam
140
147
}
141
148
142
149
if ($ request ->attributes ->has ($ name )) {
150
+ $ request ->attributes ->set ('_entity_used_attributes ' , array_merge ($ request ->attributes ->get ('_entity_used_attributes ' ) ?? [], [$ name ]));
151
+
143
152
return $ request ->attributes ->get ($ name ) ?? ($ options ->stripNull ? false : null );
144
153
}
145
154
146
155
if (!$ options ->id && $ request ->attributes ->has ('id ' )) {
156
+ $ request ->attributes ->set ('_entity_used_attributes ' , array_merge ($ request ->attributes ->get ('_entity_used_attributes ' ) ?? [], ['id ' ]));
157
+
147
158
return $ request ->attributes ->get ('id ' ) ?? ($ options ->stripNull ? false : null );
148
159
}
149
160
@@ -152,11 +163,15 @@ private function getIdentifier(Request $request, MapEntity $options, string $nam
152
163
153
164
private function getCriteria (Request $ request , MapEntity $ options , ObjectManager $ manager ): array
154
165
{
155
- if (null === $ mapping = $ options ->mapping ) {
156
- $ mapping = $ request ->attributes ->keys ();
166
+ $ singleKey = [] === $ options ->mapping ;
167
+
168
+ if ($ autoMapping = !$ mapping = $ options ->mapping ) {
169
+ $ mapping = $ request ->attributes ->all ();
170
+ unset($ mapping ['_entity_used_attributes ' ]);
171
+ $ mapping = array_keys ($ mapping );
157
172
}
158
173
159
- if ($ mapping && \is_array ( $ mapping ) && array_is_list ($ mapping )) {
174
+ if ($ mapping && array_is_list ($ mapping )) {
160
175
$ mapping = array_combine ($ mapping , $ mapping );
161
176
}
162
177
@@ -176,13 +191,33 @@ private function getCriteria(Request $request, MapEntity $options, ObjectManager
176
191
177
192
$ criteria = [];
178
193
$ metadata = $ manager ->getClassMetadata ($ options ->class );
194
+ $ usedAttributes = [];
195
+
196
+ if ($ singleKey ) {
197
+ foreach ($ request ->attributes ->get ('_entity_used_attributes ' ) ?? [] as $ attribute ) {
198
+ unset($ mapping [$ attribute ]);
199
+ }
200
+ }
179
201
180
202
foreach ($ mapping as $ attribute => $ field ) {
181
203
if (!$ metadata ->hasField ($ field ) && (!$ metadata ->hasAssociation ($ field ) || !$ metadata ->isSingleValuedAssociation ($ field ))) {
204
+ if (!$ autoMapping ) {
205
+ throw new NearMissValueResolverException (sprintf ('Invalid mapping in #[MapEntity] attribute: there are no field or association named "%s" for entities of type "%s". ' , $ field , $ options ->class ));
206
+ }
207
+
182
208
continue ;
183
209
}
184
210
211
+ $ usedAttributes [] = $ attribute ;
185
212
$ criteria [$ field ] = $ request ->attributes ->get ($ attribute );
213
+
214
+ if ($ singleKey ) {
215
+ break ;
216
+ }
217
+ }
218
+
219
+ if ($ usedAttributes ) {
220
+ $ request ->attributes ->set ('_entity_used_attributes ' , array_merge ($ request ->attributes ->get ('_entity_used_attributes ' ) ?? [], $ usedAttributes ));
186
221
}
187
222
188
223
if ($ options ->stripNull ) {
0 commit comments