@@ -60,9 +60,9 @@ public function resolve(Request $request, ArgumentMetadata $argument): array
6060 $ message = sprintf (' The expression "%s" returned null. ' , $ options ->expr );
6161 }
6262 // find by identifier?
63- } elseif (false === $ object = $ this ->find ($ manager , $ request , $ options , $ argument-> getName () )) {
63+ } elseif (false === $ object = $ this ->find ($ manager , $ request , $ options , $ argument )) {
6464 // find by criteria
65- if (!$ criteria = $ this ->getCriteria ($ request , $ options , $ manager )) {
65+ if (!$ criteria = $ this ->getCriteria ($ request , $ options , $ manager, $ argument )) {
6666 return [];
6767 }
6868 try {
@@ -94,13 +94,13 @@ private function getManager(?string $name, string $class): ?ObjectManager
9494 return $ manager ->getMetadataFactory ()->isTransient ($ class ) ? null : $ manager ;
9595 }
9696
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
9898 {
9999 if ($ options ->mapping || $ options ->exclude ) {
100100 return false ;
101101 }
102102
103- $ id = $ this ->getIdentifier ($ request , $ options , $ name );
103+ $ id = $ this ->getIdentifier ($ request , $ options , $ argument );
104104 if (false === $ id || null === $ id ) {
105105 return $ id ;
106106 }
@@ -119,14 +119,14 @@ private function find(ObjectManager $manager, Request $request, MapEntity $optio
119119 }
120120 }
121121
122- private function getIdentifier (Request $ request , MapEntity $ options , string $ name ): mixed
122+ private function getIdentifier (Request $ request , MapEntity $ options , ArgumentMetadata $ argument ): mixed
123123 {
124124 if (\is_array ($ options ->id )) {
125125 $ id = [];
126126 foreach ($ options ->id as $ field ) {
127127 // Convert "%s_uuid" to "foobar_uuid"
128128 if (str_contains ($ field , '%s ' )) {
129- $ field = sprintf ($ field , $ name );
129+ $ field = sprintf ($ field , $ argument -> getName () );
130130 }
131131
132132 $ id [$ field ] = $ request ->attributes ->get ($ field );
@@ -135,28 +135,54 @@ private function getIdentifier(Request $request, MapEntity $options, string $nam
135135 return $ id ;
136136 }
137137
138- if (null !== $ options ->id ) {
139- $ name = $ options ->id ;
138+ if ($ options ->id ) {
139+ return $ request -> attributes -> get ( $ options -> id ) ?? ( $ options ->stripNull ? false : null ) ;
140140 }
141141
142+ $ name = $ argument ->getName ();
143+
142144 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 );
144158 }
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 ());
145161
146- if (!$ options ->id && $ request ->attributes ->has ('id ' )) {
147162 return $ request ->attributes ->get ('id ' ) ?? ($ options ->stripNull ? false : null );
148163 }
149164
150165 return false ;
151166 }
152167
153- private function getCriteria (Request $ request , MapEntity $ options , ObjectManager $ manager ): array
168+ private function getCriteria (Request $ request , MapEntity $ options , ObjectManager $ manager, ArgumentMetadata $ argument ): array
154169 {
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 ());
156182 $ mapping = $ request ->attributes ->keys ();
157183 }
158184
159- if ($ mapping && \is_array ( $ mapping ) && array_is_list ($ mapping )) {
185+ if ($ mapping && array_is_list ($ mapping )) {
160186 $ mapping = array_combine ($ mapping , $ mapping );
161187 }
162188
@@ -168,17 +194,11 @@ private function getCriteria(Request $request, MapEntity $options, ObjectManager
168194 return [];
169195 }
170196
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-
177197 $ criteria = [];
178- $ metadata = $ manager ->getClassMetadata ($ options ->class );
198+ $ metadata = null === $ options -> mapping ? $ manager ->getClassMetadata ($ options ->class ) : false ;
179199
180200 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 ))) {
182202 continue ;
183203 }
184204
0 commit comments