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