diff --git a/doctrine.rst b/doctrine.rst index 770a7b32a0a..2c47d0c5744 100644 --- a/doctrine.rst +++ b/doctrine.rst @@ -781,6 +781,47 @@ variable. Let's say you want the first or the last comment of a product dependin ): Response { } +If you need to get other information from other services, you can also inject variables in your expression +thanks to the ``VariableInjector`` service. By default only user is injected :: + + #[Route('/product/{id}/comments')] + public function show( + #[MapEntity(class: Post::class, expr: 'repository.findBy({"author": user.id}, {}, 10)')] + iterable $posts + ): Response { + } + +If you need other services in the Expression you can alias original service :: + + #[AsAlias(id: 'doctrine.orm.entity_value_resolver_variable_injector')] + class VariableInjector implements EntityValueResolverVariablesInjectorInterface + { + public function __construct(private TokenStorageInterface $tokenStorage, private MyService $myService) + { + } + + public function getVariables(): array + { + return [ + 'user' => $this->tokenStorage?->getToken()?->getUser(), + 'my_variable' => $this->myService->getMyVariable(), + ]; + } + } + +and getting your variable in MapEntity Expression :: + + #[Route('/product/{id}/comments')] + public function show( + #[MapEntity(class: Post::class, expr: 'repository.findBy({"author": user.id, "other": my_variable}, {}, 10)')] + iterable $posts + ): Response { + } + +.. versionadded:: 7.2 + + The variable injector was introduced in Symfony 7.2. + MapEntity Options ~~~~~~~~~~~~~~~~~