From 09ca76ea0b7693696fdf38f2b9b91949869b14a7 Mon Sep 17 00:00:00 2001 From: eltharin Date: Mon, 8 Jul 2024 14:08:43 +0200 Subject: [PATCH 1/3] add value resolver variable loader --- doctrine.rst | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/doctrine.rst b/doctrine.rst index 770a7b32a0a..f3914e4c2c8 100644 --- a/doctrine.rst +++ b/doctrine.rst @@ -781,6 +781,43 @@ 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 ``VariableLoader`` 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_variables_loader')] + class VariableLoader implements EntityValueResolverVariablesLoaderInterface + { + 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 { + } + MapEntity Options ~~~~~~~~~~~~~~~~~ From 95bddff80120ef224679861fe53d0584f724acc7 Mon Sep 17 00:00:00 2001 From: eltharin Date: Mon, 8 Jul 2024 14:12:21 +0200 Subject: [PATCH 2/3] add version --- doctrine.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/doctrine.rst b/doctrine.rst index f3914e4c2c8..85b2aee6541 100644 --- a/doctrine.rst +++ b/doctrine.rst @@ -818,6 +818,10 @@ If you need other services in the Expression you can alias original service :: ): Response { } +.. versionadded:: 7.2 + + The variable loader was introduced in Symfony 7.2. + MapEntity Options ~~~~~~~~~~~~~~~~~ From 535906440edac45864f5f91c21362a8fbaf9c73e Mon Sep 17 00:00:00 2001 From: eltharin Date: Mon, 8 Jul 2024 17:11:46 +0200 Subject: [PATCH 3/3] change loader to injector --- doctrine.rst | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/doctrine.rst b/doctrine.rst index 85b2aee6541..2c47d0c5744 100644 --- a/doctrine.rst +++ b/doctrine.rst @@ -782,7 +782,7 @@ variable. Let's say you want the first or the last comment of a product dependin } If you need to get other information from other services, you can also inject variables in your expression -thanks to the ``VariableLoader`` service. By default only user is injected :: +thanks to the ``VariableInjector`` service. By default only user is injected :: #[Route('/product/{id}/comments')] public function show( @@ -793,8 +793,8 @@ thanks to the ``VariableLoader`` service. By default only user is injected :: If you need other services in the Expression you can alias original service :: - #[AsAlias(id: 'doctrine.orm.entity_value_resolver_variables_loader')] - class VariableLoader implements EntityValueResolverVariablesLoaderInterface + #[AsAlias(id: 'doctrine.orm.entity_value_resolver_variable_injector')] + class VariableInjector implements EntityValueResolverVariablesInjectorInterface { public function __construct(private TokenStorageInterface $tokenStorage, private MyService $myService) { @@ -809,7 +809,7 @@ If you need other services in the Expression you can alias original service :: } } - and getting your variable in MapEntity Expression :: +and getting your variable in MapEntity Expression :: #[Route('/product/{id}/comments')] public function show( @@ -820,7 +820,7 @@ If you need other services in the Expression you can alias original service :: .. versionadded:: 7.2 - The variable loader was introduced in Symfony 7.2. + The variable injector was introduced in Symfony 7.2. MapEntity Options ~~~~~~~~~~~~~~~~~