@@ -637,8 +637,22 @@ automatically! You can simplify the controller to::
637
637
}
638
638
}
639
639
640
- That's it! The bundle uses the ``{id} `` from the route to query for the ``Product ``
641
- by the ``id `` column. If it's not found, a 404 page is generated.
640
+ That's it! The bundle uses the ``{id} `` from the route to query for the ``Product `` by its
641
+ primary key (using ``find() ``). If no matching object is found, a 404 page is generated.
642
+
643
+ .. tip ::
644
+
645
+ This works best if you only have a single route parameter. It is however recommended
646
+ to match the route parameter name with the controller argument name. This will allow the
647
+ use of multiple parameters without any ambiguity. When a route parameter name matches
648
+ with the controller argument name, it will use its value to query on its primary key::
649
+
650
+ #[Route('/product/{product}/shop/{shop}')]
651
+ public function show(Product $product, Shop $shop): Response
652
+ {
653
+ // use the Product and Shop!
654
+ // ...
655
+ }
642
656
643
657
.. tip ::
644
658
@@ -660,16 +674,26 @@ Fetch Automatically
660
674
If your route wildcards match properties on your entity, then the resolver
661
675
will automatically fetch them::
662
676
677
+ /**
678
+ * Fetch via primary key because {product} matches with the controller
679
+ * argument name.
680
+ */
681
+ #[Route('/product/{product}')]
682
+ public function showByPk(Product $product): Response
683
+ {
684
+ }
685
+
663
686
/**
664
687
* Fetch via primary key because {id} is in the route.
665
688
*/
666
689
#[Route('/product/{id}')]
667
- public function showByPk (Product $product): Response
690
+ public function showById (Product $product): Response
668
691
{
669
692
}
670
693
671
694
/**
672
695
* Perform a findOneBy() where the slug property matches {slug}.
696
+ * Requires doctrine.orm.controller_resolver.auto_mapping to set to true.
673
697
*/
674
698
#[Route('/product/{slug}')]
675
699
public function showBySlug(Product $product): Response
@@ -678,21 +702,29 @@ will automatically fetch them::
678
702
679
703
Automatic fetching works in these situations:
680
704
681
- * If ``{id} `` is in your route, then this is used to fetch by
682
- primary key via the ``find() `` method.
705
+ * If the route parameter name matches with the controller argument name,
706
+ it will be used to fetch by primary key via the ``find() `` method.
707
+
708
+ * If ``{id} `` is in your route, then this is used to fetch by primary key
709
+ via the ``find() `` method. This is only recommended when you have a single
710
+ route parameter that needs to be resolved, as otherwise the value of ``{id} ``
711
+ will be used to resolve both controller arguments.
683
712
684
- * The resolver will attempt to do a ``findOneBy() `` fetch by using
685
- *all * of the wildcards in your route that are actually properties
686
- on your entity (non-properties are ignored).
713
+ * If ``doctrine.orm.controller_resolver.auto_mapping `` is set to ``true ``
714
+ (enabled by default when using DoctrineBundle before 2.12, but not recommended),
715
+ the resolver will attempt to do a ``findOneBy() `` fetch by using *all * of the wildcards in
716
+ your route that are actually properties on your entity (non-properties are ignored).
717
+
718
+ .. caution ::
687
719
688
- This behavior is enabled by default on all controllers. If you prefer, you can
689
- restrict this feature to only work on route wildcards called `` id `` to look for
690
- entities by primary key. To do so, set the option
691
- `` doctrine.orm.controller_resolver.auto_mapping `` to `` false `` .
720
+ It is not recommended to enable `` doctrine.orm.controller_resolver.auto_mapping ``
721
+ as it can lead to surprising behavior and has been been disabled by default
722
+ with DoctrineBundle 3 for that reason. Use the `` MapEntity `` `` mapping `` option
723
+ to configure custom mapping requirements for specific routes when needed .
692
724
693
- When `` auto_mapping `` is disabled, you can configure the mapping explicitly for
694
- any controller argument with the ``MapEntity `` attribute. You can even control
695
- the `` EntityValueResolver `` behavior by using the `MapEntity options `_ ::
725
+ You can manually override the mapping for any controller argument with the
726
+ ``MapEntity `` attribute. You can even control the `` EntityValueResolver ``
727
+ behavior by using the `MapEntity options `_ ::
696
728
697
729
// src/Controller/ProductController.php
698
730
namespace App\Controller;
@@ -771,7 +803,7 @@ control behavior:
771
803
772
804
``id ``
773
805
If an ``id `` option is configured and matches a route parameter, then
774
- the resolver will find by the primary key::
806
+ the resolver will use that value to fetch by the primary key::
775
807
776
808
#[Route('/product/{product_id}')]
777
809
public function show(
0 commit comments