Skip to content

Commit f32ea5f

Browse files
committed
Merge branch '6.4' into 7.0
* 6.4: [Doctrine] The use of `doctrine.orm.controller_resolver.auto_mapping option`
2 parents 7700c58 + 62ec84e commit f32ea5f

File tree

1 file changed

+31
-32
lines changed

1 file changed

+31
-32
lines changed

doctrine.rst

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -636,34 +636,6 @@ automatically! You can simplify the controller to::
636636
That's it! The bundle uses the ``{id}`` from the route to query for the ``Product``
637637
by the ``id`` column. If it's not found, a 404 page is generated.
638638

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-
667639
.. tip::
668640

669641
When enabled globally, it's possible to disable the behavior on a specific
@@ -709,14 +681,41 @@ Automatic fetching works in these situations:
709681
*all* of the wildcards in your route that are actually properties
710682
on your entity (non-properties are ignored).
711683

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

715714
Fetch via an Expression
716715
~~~~~~~~~~~~~~~~~~~~~~~
717716

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>`::
720719

721720
#[Route('/product/{product_id}')]
722721
public function show(

0 commit comments

Comments
 (0)