Skip to content

Commit fd7bcb8

Browse files
chadyredjaviereguiluz
authored andcommitted
[Doctrine] The use of doctrine.orm.controller_resolver.auto_mapping option
1 parent ccaf3a1 commit fd7bcb8

File tree

1 file changed

+32
-30
lines changed

1 file changed

+32
-30
lines changed

doctrine.rst

+32-30
Original file line numberDiff line numberDiff line change
@@ -640,34 +640,6 @@ automatically! You can simplify the controller to::
640640
That's it! The bundle uses the ``{id}`` from the route to query for the ``Product``
641641
by the ``id`` column. If it's not found, a 404 page is generated.
642642

643-
This behavior is enabled by default on all your controllers. You can
644-
disable it by setting the ``doctrine.orm.controller_resolver.auto_mapping``
645-
config option to ``false``.
646-
647-
When disabled, you can enable it individually on the desired controllers by
648-
using the ``MapEntity`` attribute::
649-
650-
// src/Controller/ProductController.php
651-
namespace App\Controller;
652-
653-
use App\Entity\Product;
654-
use Symfony\Bridge\Doctrine\Attribute\MapEntity;
655-
use Symfony\Component\HttpFoundation\Response;
656-
use Symfony\Component\Routing\Annotation\Route;
657-
// ...
658-
659-
class ProductController extends AbstractController
660-
{
661-
#[Route('/product/{id}')]
662-
public function show(
663-
#[MapEntity]
664-
Product $product
665-
): Response {
666-
// use the Product!
667-
// ...
668-
}
669-
}
670-
671643
.. tip::
672644

673645
When enabled globally, it's possible to disable the behavior on a specific
@@ -713,8 +685,38 @@ Automatic fetching works in these situations:
713685
*all* of the wildcards in your route that are actually properties
714686
on your entity (non-properties are ignored).
715687

716-
You can control this behavior by actually *adding* the ``MapEntity``
717-
attribute and using the `MapEntity options`_.
688+
This behavior is enabled by default on all your controllers.
689+
690+
You can only allow the use of the primary key ``id`` as a lookup placeholder
691+
for the routes parameters by setting ``doctrine.orm.controller_resolver.auto_mapping``
692+
config option to ``false``. The others entity attributes than ``id`` can't be used
693+
to autowire the entity.
694+
695+
When disabled, you can enable the entity autowiring individually on the desired controllers
696+
by using the ``MapEntity`` attribute. You can control ``EntityValueResolver`` behavior
697+
with it by using the `MapEntity options`_ ::
698+
699+
// src/Controller/ProductController.php
700+
namespace App\Controller;
701+
702+
use App\Entity\Product;
703+
use Symfony\Bridge\Doctrine\Attribute\MapEntity;
704+
use Symfony\Component\HttpFoundation\Response;
705+
use Symfony\Component\Routing\Annotation\Route;
706+
// ...
707+
708+
class ProductController extends AbstractController
709+
{
710+
#[Route('/product/{slug}')]
711+
public function show(
712+
#[MapEntity(mapping: ['slug' => 'slug'])]
713+
Product $product
714+
): Response {
715+
// use the Product!
716+
// ...
717+
}
718+
}
719+
718720

719721
Fetch via an Expression
720722
~~~~~~~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)