Skip to content

Commit

Permalink
Adjust EntityValueResolver auto mapping documentation
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bobvandevijver committed Mar 14, 2024
1 parent 05e86b4 commit 0bbccbc
Showing 1 changed file with 47 additions and 15 deletions.
62 changes: 47 additions & 15 deletions doctrine.rst
Original file line number Diff line number Diff line change
Expand Up @@ -637,8 +637,22 @@ automatically! You can simplify the controller to::
}
}

That's it! The bundle uses the ``{id}`` from the route to query for the ``Product``
by the ``id`` column. If it's not found, a 404 page is generated.
That's it! The bundle uses the ``{id}`` from the route to query for the ``Product`` by its
primary key (using ``find()``). If no matching object is found, a 404 page is generated.

.. tip::

This works best if you only have a single route parameter. It is however recommended
to match the route parameter name with the controller argument name. This will allow the
use of multiple parameters without any ambiguity. When a route parameter name matches
with the controller argument name, it will use its value to query on its primary key.

#[Route('/product/{product}/shop/{shop}')]
public function show(Product $product, Shop $shop): Response
{
// use the Product and Shop!
// ...
}

.. tip::

Expand All @@ -660,6 +674,15 @@ Fetch Automatically
If your route wildcards match properties on your entity, then the resolver
will automatically fetch them::

/**
* Fetch via primary key because {product} matches with the controller
* argument name.
*/
#[Route('/product/{product}')]
public function showByPk(Product $product): Response
{
}

/**
* Fetch via primary key because {id} is in the route.
*/
Expand All @@ -670,6 +693,7 @@ will automatically fetch them::

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

Automatic fetching works in these situations:

* If ``{id}`` is in your route, then this is used to fetch by
primary key via the ``find()`` method.
* If the route parameter name matches with the controller argument name,
it will be used to fetch by primary key via the ``find()`` method.

* If ``{id}`` is in your route, then this is used to fetch by primary key
via the ``find()`` method. This is only recommended when you have a single
route parameter that needs to be resolved, as otherwise the value of ``{id}``
will be used to resolve both controller arguments.

* If ``doctrine.orm.controller_resolver.auto_mapping`` is set to ``true``
(enabled by default when using DoctrineBundle before 2.12, but not recommended),
the resolver will attempt to do a ``findOneBy()`` fetch by using *all* of the wildcards in
your route that are actually properties on your entity (non-properties are ignored).

* The resolver will attempt to do a ``findOneBy()`` fetch by using
*all* of the wildcards in your route that are actually properties
on your entity (non-properties are ignored).
caution::

This behavior is enabled by default on all controllers. If you prefer, you can
restrict this feature to only work on route wildcards called ``id`` to look for
entities by primary key. To do so, set the option
``doctrine.orm.controller_resolver.auto_mapping`` to ``false``.
It is not recommended to enable ``doctrine.orm.controller_resolver.auto_mapping``
as it can lead to surprising behaviour and has been been disabled by default

Check failure on line 721 in doctrine.rst

View workflow job for this annotation

GitHub Actions / Lint (DOCtor-RST)

Please use American English for: behaviour
with DoctrineBundle 3 for that reason. Use the ``MapEntity`` ``mapping`` option
to configure custom mapping requirements for specific routes when needed.

When ``auto_mapping`` is disabled, you can configure the mapping explicitly for
any controller argument with the ``MapEntity`` attribute. You can even control
the ``EntityValueResolver`` behavior by using the `MapEntity options`_ ::
You can manually override the mapping for any controller argument with the
``MapEntity`` attribute. You can even control the ``EntityValueResolver``
behavior by using the `MapEntity options`_ ::

// src/Controller/ProductController.php
namespace App\Controller;
Expand Down Expand Up @@ -771,7 +803,7 @@ control behavior:

``id``
If an ``id`` option is configured and matches a route parameter, then
the resolver will find by the primary key::
the resolver will use that value to fetch by the primary key::

#[Route('/product/{product_id}')]
public function show(
Expand Down

0 comments on commit 0bbccbc

Please sign in to comment.