Skip to content

Commit dbb0f13

Browse files
committed
Add documentation about the Doctrine Entity Value Resolver
1 parent 1544a18 commit dbb0f13

File tree

2 files changed

+105
-14
lines changed

2 files changed

+105
-14
lines changed

best_practices.rst

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -246,16 +246,17 @@ Instead, you must use dependency injection to fetch services by
246246
:ref:`type-hinting action method arguments <controller-accessing-services>` or
247247
constructor arguments.
248248

249-
Use ParamConverters If They Are Convenient
250-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
249+
Use EntityValueResolver If It Is Convenient
250+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
251251

252252
If you're using :doc:`Doctrine </doctrine>`, then you can *optionally* use the
253-
`ParamConverter`_ to automatically query for an entity and pass it as an argument
254-
to your controller. It will also show a 404 page if no entity can be found.
253+
`EntityValueResolver`_ to automatically query for an entity and pass it as an
254+
argument to your controller. It will also show a 404 page if no entity can be
255+
found.
255256

256257
If the logic to get an entity from a route variable is more complex, instead of
257-
configuring the ParamConverter, it's better to make the Doctrine query inside
258-
the controller (e.g. by calling to a :doc:`Doctrine repository method </doctrine>`).
258+
configuring the EntityValueResolver, it's better to make the Doctrine query
259+
inside the controller (e.g. by calling to a :doc:`Doctrine repository method </doctrine>`).
259260

260261
Templates
261262
---------

doctrine.rst

Lines changed: 98 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -598,15 +598,59 @@ the :ref:`doctrine-queries` section.
598598
see the web debug toolbar, install the ``profiler`` :ref:`Symfony pack <symfony-packs>`
599599
by running this command: ``composer require --dev symfony/profiler-pack``.
600600

601-
Automatically Fetching Objects (ParamConverter)
602-
-----------------------------------------------
601+
Automatically Fetching Objects (EntityValueResolver)
602+
----------------------------------------------------
603603

604-
In many cases, you can use the `SensioFrameworkExtraBundle`_ to do the query
605-
for you automatically! First, install the bundle in case you don't have it:
604+
In many cases, you can use the `EntityValueResolver`_ to do the query for you
605+
automatically! First, enable the feature:
606606

607-
.. code-block:: terminal
607+
.. configuration-block::
608+
609+
.. code-block:: yaml
610+
611+
# config/packages/doctrine.yaml
612+
doctrine:
613+
orm:
614+
controller_resolver:
615+
enabled: true
616+
auto_mapping: true
617+
evict_cache: false
618+
619+
.. code-block:: xml
620+
621+
<!-- config/packages/doctrine.xml -->
622+
<?xml version="1.0" encoding="UTF-8" ?>
623+
<container xmlns="http://symfony.com/schema/dic/services"
624+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
625+
xmlns:doctrine="http://symfony.com/schema/dic/doctrine"
626+
xsi:schemaLocation="http://symfony.com/schema/dic/services
627+
https://symfony.com/schema/dic/services/services-1.0.xsd
628+
http://symfony.com/schema/dic/doctrine
629+
https://symfony.com/schema/dic/doctrine/doctrine-1.0.xsd">
630+
631+
<doctrine:config>
632+
<!-- by convention the env var names are always uppercase -->
633+
<doctrine:orm>
634+
<doctrine:controller_resolver auto_mapping="true" evict_cache="false"/>
635+
</doctrine:orm>
636+
</doctrine:config>
637+
638+
</container>
639+
640+
.. code-block:: php
641+
642+
// config/packages/doctrine.php
643+
use Symfony\Config\DoctrineConfig;
608644
609-
$ composer require sensio/framework-extra-bundle
645+
return static function (DoctrineConfig $doctrine) {
646+
$controllerResolver = $doctrine->orm()
647+
->entityManager('default')
648+
// ...
649+
->controllerResolver();
650+
651+
$controllerResolver->autoMapping(true);
652+
$controllerResolver->evictCache(true);
653+
};
610654
611655
Now, simplify your controller::
612656

@@ -632,7 +676,54 @@ Now, simplify your controller::
632676
That's it! The bundle uses the ``{id}`` from the route to query for the ``Product``
633677
by the ``id`` column. If it's not found, a 404 page is generated.
634678

635-
There are many more options you can use. Read more about the `ParamConverter`_.
679+
This behavior can be enabled on all your controllers, by setting the ``auto_mapping``
680+
parameter to ``true``. Or individually on the desired controllers by using the
681+
``MapEntity`` attribute:
682+
683+
// src/Controller/ProductController.php
684+
namespace App\Controller;
685+
686+
use App\Entity\Product;
687+
use App\Repository\ProductRepository;
688+
use Symfony\Bridge\Doctrine\Attribute\MapEntity;
689+
use Symfony\Component\HttpFoundation\Response;
690+
use Symfony\Component\Routing\Annotation\Route;
691+
// ...
692+
693+
class ProductController extends AbstractController
694+
{
695+
#[Route('/product/{id}', name: 'product_show')]
696+
public function show(
697+
#[MapEntity]
698+
Product $product
699+
): Response {
700+
// use the Product!
701+
// ...
702+
}
703+
}
704+
705+
.. tip::
706+
707+
When enabled globally, it's possible to disabled the behavior on a specific
708+
controller, by using the ``MapEntity`` set to ``disabled``.
709+
710+
public function show(
711+
#[CurrentUser]
712+
#[MapEntity(disabled: true)]
713+
User $user
714+
): Response {
715+
// User is not resolved by the EntityValueResolver
716+
// ...
717+
}
718+
719+
.. versionadded:: 6.2
720+
721+
Entity Value Resolver was introduced in Symfony 6.2.
722+
723+
.. versionadded:: 2.7.1
724+
725+
Autowiring of the EntityValueResolver was introduced in DoctrineBundle
726+
2.7.1.
636727

637728
Updating an Object
638729
------------------
@@ -896,7 +987,6 @@ Learn more
896987
.. _`DoctrineMigrationsBundle`: https://github.com/doctrine/DoctrineMigrationsBundle
897988
.. _`NativeQuery`: https://www.doctrine-project.org/projects/doctrine-orm/en/current/reference/native-sql.html
898989
.. _`SensioFrameworkExtraBundle`: https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/index.html
899-
.. _`ParamConverter`: https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html
900990
.. _`limit of 767 bytes for the index key prefix`: https://dev.mysql.com/doc/refman/5.6/en/innodb-limits.html
901991
.. _`Doctrine screencast series`: https://symfonycasts.com/screencast/symfony-doctrine
902992
.. _`API Platform`: https://api-platform.com/docs/core/validation/

0 commit comments

Comments
 (0)