Skip to content

Commit e1fd4ac

Browse files
committed
Merge branch '7.0' into 7.1
* 7.0: [Doctrine] The use of `doctrine.orm.controller_resolver.auto_mapping option`
2 parents 9200491 + f32ea5f commit e1fd4ac

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
@@ -637,34 +637,6 @@ automatically! You can simplify the controller to::
637637
That's it! The bundle uses the ``{id}`` from the route to query for the ``Product``
638638
by the ``id`` column. If it's not found, a 404 page is generated.
639639

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

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

713-
You can control this behavior by actually *adding* the ``MapEntity``
714-
attribute and using the `MapEntity options`_.
685+
This behavior is enabled by default on all controllers. If you prefer, you can
686+
restrict this feature to only work on route wildcards called ``id`` to look for
687+
entities by primary key. To do so, set the option
688+
``doctrine.orm.controller_resolver.auto_mapping`` to ``false``.
689+
690+
When ``auto_mapping`` is disabled, you can configure the mapping explicitly for
691+
any controller argument with the ``MapEntity`` attribute. You can even control
692+
the ``EntityValueResolver`` behavior by using the `MapEntity options`_ ::
693+
694+
// src/Controller/ProductController.php
695+
namespace App\Controller;
696+
697+
use App\Entity\Product;
698+
use Symfony\Bridge\Doctrine\Attribute\MapEntity;
699+
use Symfony\Component\HttpFoundation\Response;
700+
use Symfony\Component\Routing\Annotation\Route;
701+
// ...
702+
703+
class ProductController extends AbstractController
704+
{
705+
#[Route('/product/{slug}')]
706+
public function show(
707+
#[MapEntity(mapping: ['slug' => 'slug'])]
708+
Product $product
709+
): Response {
710+
// use the Product!
711+
// ...
712+
}
713+
}
715714

716715
Fetch via an Expression
717716
~~~~~~~~~~~~~~~~~~~~~~~
718717

719-
If automatic fetching doesn't work, you can write an expression using the
720-
:doc:`ExpressionLanguage component </components/expression_language>`::
718+
If automatic fetching doesn't work for your use case, you can write an expression
719+
using the :doc:`ExpressionLanguage component </components/expression_language>`::
721720

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

0 commit comments

Comments
 (0)