Skip to content

[Doctrine] The use of doctrine.orm.controller_resolver.auto_mapping #19408

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 32 additions & 30 deletions doctrine.rst
Original file line number Diff line number Diff line change
Expand Up @@ -640,34 +640,6 @@ 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.

This behavior is enabled by default on all your controllers. You can
disable it by setting the ``doctrine.orm.controller_resolver.auto_mapping``
config option to ``false``.

When disabled, you can enable it individually on the desired controllers by
using the ``MapEntity`` attribute::

// src/Controller/ProductController.php
namespace App\Controller;

use App\Entity\Product;
use Symfony\Bridge\Doctrine\Attribute\MapEntity;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
// ...

class ProductController extends AbstractController
{
#[Route('/product/{id}')]
public function show(
#[MapEntity]
Product $product
): Response {
// use the Product!
// ...
}
}

.. tip::

When enabled globally, it's possible to disable the behavior on a specific
Expand Down Expand Up @@ -713,8 +685,38 @@ Automatic fetching works in these situations:
*all* of the wildcards in your route that are actually properties
on your entity (non-properties are ignored).

You can control this behavior by actually *adding* the ``MapEntity``
attribute and using the `MapEntity options`_.
This behavior is enabled by default on all your controllers.

You can only allow the use of the primary key ``id`` as a lookup placeholder
for the routes parameters by setting ``doctrine.orm.controller_resolver.auto_mapping``
config option to ``false``. The others entity attributes than ``id`` can't be used
to autowire the entity.

When disabled, you can enable the entity autowiring individually on the desired controllers
by using the ``MapEntity`` attribute. You can control ``EntityValueResolver`` behavior
with it by using the `MapEntity options`_ ::

// src/Controller/ProductController.php
namespace App\Controller;

use App\Entity\Product;
use Symfony\Bridge\Doctrine\Attribute\MapEntity;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
// ...

class ProductController extends AbstractController
{
#[Route('/product/{slug}')]
public function show(
#[MapEntity(mapping: ['slug' => 'slug'])]
Product $product
): Response {
// use the Product!
// ...
}
}


Fetch via an Expression
~~~~~~~~~~~~~~~~~~~~~~~
Expand Down