Skip to content

Commit 36be15a

Browse files
chadyredcarte-qsd
authored andcommitted
[Doctrine] The use of doctrine.orm.controller_resolver.auto_mapping option
1 parent 9e49d97 commit 36be15a

File tree

1 file changed

+32
-30
lines changed

1 file changed

+32
-30
lines changed

doctrine.rst

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

642-
This behavior is enabled by default on all your controllers. You can
643-
disable it by setting the ``doctrine.orm.controller_resolver.auto_mapping``
644-
config option to ``false``.
645-
646-
When disabled, you can enable it individually on the desired controllers by
647-
using the ``MapEntity`` attribute::
648-
649-
// src/Controller/ProductController.php
650-
namespace App\Controller;
651-
652-
use App\Entity\Product;
653-
use Symfony\Bridge\Doctrine\Attribute\MapEntity;
654-
use Symfony\Component\HttpFoundation\Response;
655-
use Symfony\Component\Routing\Annotation\Route;
656-
// ...
657-
658-
class ProductController extends AbstractController
659-
{
660-
#[Route('/product/{id}')]
661-
public function show(
662-
#[MapEntity]
663-
Product $product
664-
): Response {
665-
// use the Product!
666-
// ...
667-
}
668-
}
669-
670642
.. tip::
671643

672644
When enabled globally, it's possible to disable the behavior on a specific
@@ -712,8 +684,38 @@ Automatic fetching works in these situations:
712684
*all* of the wildcards in your route that are actually properties
713685
on your entity (non-properties are ignored).
714686

715-
You can control this behavior by actually *adding* the ``MapEntity``
716-
attribute and using the `MapEntity options`_.
687+
This behavior is enabled by default on all your controllers.
688+
689+
You can only allow the use of the primary key ``id`` as a lookup placeholder
690+
as a route parameter by setting ``doctrine.orm.controller_resolver.auto_mapping``
691+
config option to ``false``. The others attributes will not be used to autowire
692+
the entity.
693+
694+
When disabled, you can enable it individually on the desired controllers by
695+
using the ``MapEntity`` attribute. You can control `EntityValueResolver` behavior
696+
with it by using the `MapEntity options`_ ::
697+
698+
// src/Controller/ProductController.php
699+
namespace App\Controller;
700+
701+
use App\Entity\Product;
702+
use Symfony\Bridge\Doctrine\Attribute\MapEntity;
703+
use Symfony\Component\HttpFoundation\Response;
704+
use Symfony\Component\Routing\Annotation\Route;
705+
// ...
706+
707+
class ProductController extends AbstractController
708+
{
709+
#[Route('/product/{slug}')]
710+
public function show(
711+
#[MapEntity(mapping: ['slug' => 'slug'])]
712+
Product $product
713+
): Response {
714+
// use the Product!
715+
// ...
716+
}
717+
}
718+
717719

718720
Fetch via an Expression
719721
~~~~~~~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)