@@ -636,34 +636,6 @@ automatically! You can simplify the controller to::
636
636
That's it! The bundle uses the ``{id} `` from the route to query for the ``Product ``
637
637
by the ``id `` column. If it's not found, a 404 page is generated.
638
638
639
- This behavior is enabled by default on all your controllers. You can
640
- disable it by setting the ``doctrine.orm.controller_resolver.auto_mapping ``
641
- config option to ``false ``.
642
-
643
- When disabled, you can enable it individually on the desired controllers by
644
- using the ``MapEntity `` attribute::
645
-
646
- // src/Controller/ProductController.php
647
- namespace App\Controller;
648
-
649
- use App\Entity\Product;
650
- use Symfony\Bridge\Doctrine\Attribute\MapEntity;
651
- use Symfony\Component\HttpFoundation\Response;
652
- use Symfony\Component\Routing\Annotation\Route;
653
- // ...
654
-
655
- class ProductController extends AbstractController
656
- {
657
- #[Route('/product/{id}')]
658
- public function show(
659
- #[MapEntity]
660
- Product $product
661
- ): Response {
662
- // use the Product!
663
- // ...
664
- }
665
- }
666
-
667
639
.. tip ::
668
640
669
641
When enabled globally, it's possible to disable the behavior on a specific
@@ -709,14 +681,41 @@ Automatic fetching works in these situations:
709
681
*all * of the wildcards in your route that are actually properties
710
682
on your entity (non-properties are ignored).
711
683
712
- You can control this behavior by actually *adding * the ``MapEntity ``
713
- attribute and using the `MapEntity options `_.
684
+ This behavior is enabled by default on all controllers. If you prefer, you can
685
+ restrict this feature to only work on route wildcards called ``id `` to look for
686
+ entities by primary key. To do so, set the option
687
+ ``doctrine.orm.controller_resolver.auto_mapping `` to ``false ``.
688
+
689
+ When ``auto_mapping `` is disabled, you can configure the mapping explicitly for
690
+ any controller argument with the ``MapEntity `` attribute. You can even control
691
+ the ``EntityValueResolver `` behavior by using the `MapEntity options `_ ::
692
+
693
+ // src/Controller/ProductController.php
694
+ namespace App\Controller;
695
+
696
+ use App\Entity\Product;
697
+ use Symfony\Bridge\Doctrine\Attribute\MapEntity;
698
+ use Symfony\Component\HttpFoundation\Response;
699
+ use Symfony\Component\Routing\Annotation\Route;
700
+ // ...
701
+
702
+ class ProductController extends AbstractController
703
+ {
704
+ #[Route('/product/{slug}')]
705
+ public function show(
706
+ #[MapEntity(mapping: ['slug' => 'slug'])]
707
+ Product $product
708
+ ): Response {
709
+ // use the Product!
710
+ // ...
711
+ }
712
+ }
714
713
715
714
Fetch via an Expression
716
715
~~~~~~~~~~~~~~~~~~~~~~~
717
716
718
- If automatic fetching doesn't work, you can write an expression using the
719
- :doc: `ExpressionLanguage component </components/expression_language >`::
717
+ If automatic fetching doesn't work for your use case , you can write an expression
718
+ using the :doc: `ExpressionLanguage component </components/expression_language >`::
720
719
721
720
#[Route('/product/{product_id}')]
722
721
public function show(
0 commit comments