Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b72f976

Browse filesBrowse files
Adjust EntityValueResolver auto mapping documentation
This reflects the changes introduced with doctrine/DoctrineBundle#1762 and changes the documentation to instruct users to not rely on the auto_mapping option as it can lead to surprising behaviour. A change for the recipe is also incoming.
1 parent 05e86b4 commit b72f976
Copy full SHA for b72f976

File tree

1 file changed

+48
-16
lines changed
Filter options

1 file changed

+48
-16
lines changed

‎doctrine.rst

Copy file name to clipboardExpand all lines: doctrine.rst
+48-16Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -637,8 +637,22 @@ automatically! You can simplify the controller to::
637637
}
638638
}
639639

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+
}
642656

643657
.. tip::
644658

@@ -660,16 +674,26 @@ Fetch Automatically
660674
If your route wildcards match properties on your entity, then the resolver
661675
will automatically fetch them::
662676

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+
663686
/**
664687
* Fetch via primary key because {id} is in the route.
665688
*/
666689
#[Route('/product/{id}')]
667-
public function showByPk(Product $product): Response
690+
public function showById(Product $product): Response
668691
{
669692
}
670693

671694
/**
672695
* Perform a findOneBy() where the slug property matches {slug}.
696+
* Requires doctrine.orm.controller_resolver.auto_mapping to set to true.
673697
*/
674698
#[Route('/product/{slug}')]
675699
public function showBySlug(Product $product): Response
@@ -678,21 +702,29 @@ will automatically fetch them::
678702

679703
Automatic fetching works in these situations:
680704

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.
683712

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::
687719

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.
692724

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`_ ::
696728

697729
// src/Controller/ProductController.php
698730
namespace App\Controller;
@@ -771,7 +803,7 @@ control behavior:
771803

772804
``id``
773805
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::
775807

776808
#[Route('/product/{product_id}')]
777809
public function show(

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.