@@ -598,15 +598,59 @@ the :ref:`doctrine-queries` section.
598
598
see the web debug toolbar, install the ``profiler `` :ref: `Symfony pack <symfony-packs >`
599
599
by running this command: ``composer require --dev symfony/profiler-pack ``.
600
600
601
- Automatically Fetching Objects (ParamConverter )
602
- -----------------------------------------------
601
+ Automatically Fetching Objects (EntityValueResolver )
602
+ ----------------------------------------------------
603
603
604
- In many cases, you can use the `SensioFrameworkExtraBundle `_ to do the query
605
- for you automatically! First, install the bundle in case you don't have it :
604
+ In many cases, you can use the `EntityValueResolver `_ to do the query for you
605
+ automatically! First, enable the feature :
606
606
607
- .. code-block :: terminal
607
+ .. configuration-block ::
608
+
609
+ .. code-block :: yaml
610
+
611
+ # config/packages/doctrine.yaml
612
+ doctrine :
613
+ orm :
614
+ controller_resolver :
615
+ enabled : true
616
+ auto_mapping : true
617
+ evict_cache : false
618
+
619
+ .. code-block :: xml
620
+
621
+ <!-- config/packages/doctrine.xml -->
622
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
623
+ <container xmlns =" http://symfony.com/schema/dic/services"
624
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
625
+ xmlns : doctrine =" http://symfony.com/schema/dic/doctrine"
626
+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
627
+ https://symfony.com/schema/dic/services/services-1.0.xsd
628
+ http://symfony.com/schema/dic/doctrine
629
+ https://symfony.com/schema/dic/doctrine/doctrine-1.0.xsd" >
630
+
631
+ <doctrine : config >
632
+ <!-- by convention the env var names are always uppercase -->
633
+ <doctrine : orm >
634
+ <doctrine : controller_resolver auto_mapping =" true" evict_cache =" false" />
635
+ </doctrine : orm >
636
+ </doctrine : config >
637
+
638
+ </container >
639
+
640
+ .. code-block :: php
641
+
642
+ // config/packages/doctrine.php
643
+ use Symfony\Config\DoctrineConfig;
608
644
609
- $ composer require sensio/framework-extra-bundle
645
+ return static function (DoctrineConfig $doctrine) {
646
+ $controllerResolver = $doctrine->orm()
647
+ ->entityManager('default')
648
+ // ...
649
+ ->controllerResolver();
650
+
651
+ $controllerResolver->autoMapping(true);
652
+ $controllerResolver->evictCache(true);
653
+ };
610
654
611
655
Now, simplify your controller::
612
656
@@ -632,7 +676,54 @@ Now, simplify your controller::
632
676
That's it! The bundle uses the ``{id} `` from the route to query for the ``Product ``
633
677
by the ``id `` column. If it's not found, a 404 page is generated.
634
678
635
- There are many more options you can use. Read more about the `ParamConverter `_.
679
+ This behavior can be enabled on all your controllers, by setting the ``auto_mapping ``
680
+ parameter to ``true ``. Or individually on the desired controllers by using the
681
+ ``MapEntity `` attribute:
682
+
683
+ // src/Controller/ProductController.php
684
+ namespace App\C ontroller;
685
+
686
+ use App\E ntity\P roduct;
687
+ use App\R epository\P roductRepository;
688
+ use Symfony\B ridge\D octrine\A ttribute\M apEntity;
689
+ use Symfony\C omponent\H ttpFoundation\R esponse;
690
+ use Symfony\C omponent\R outing\A nnotation\R oute;
691
+ // ...
692
+
693
+ class ProductController extends AbstractController
694
+ {
695
+ #[Route('/product/{id}', name: 'product_show')]
696
+ public function show(
697
+ #[MapEntity]
698
+ Product $product
699
+ ): Response {
700
+ // use the Product!
701
+ // ...
702
+ }
703
+ }
704
+
705
+ .. tip ::
706
+
707
+ When enabled globally, it's possible to disabled the behavior on a specific
708
+ controller, by using the ``MapEntity `` set to ``disabled ``.
709
+
710
+ public function show(
711
+ #[CurrentUser]
712
+ #[MapEntity(disabled: true)]
713
+ User $user
714
+ ): Response {
715
+ // User is not resolved by the EntityValueResolver
716
+ // ...
717
+ }
718
+
719
+ .. versionadded :: 6.2
720
+
721
+ Entity Value Resolver was introduced in Symfony 6.2.
722
+
723
+ .. versionadded :: 2.7.1
724
+
725
+ Autowiring of the EntityValueResolver was introduced in DoctrineBundle
726
+ 2.7.1.
636
727
637
728
Updating an Object
638
729
------------------
@@ -896,7 +987,6 @@ Learn more
896
987
.. _`DoctrineMigrationsBundle` : https://github.com/doctrine/DoctrineMigrationsBundle
897
988
.. _`NativeQuery` : https://www.doctrine-project.org/projects/doctrine-orm/en/current/reference/native-sql.html
898
989
.. _`SensioFrameworkExtraBundle` : https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/index.html
899
- .. _`ParamConverter` : https://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html
900
990
.. _`limit of 767 bytes for the index key prefix` : https://dev.mysql.com/doc/refman/5.6/en/innodb-limits.html
901
991
.. _`Doctrine screencast series` : https://symfonycasts.com/screencast/symfony-doctrine
902
992
.. _`API Platform` : https://api-platform.com/docs/core/validation/
0 commit comments