13
13
14
14
use DateTime ;
15
15
use Doctrine \DBAL \Types \Types ;
16
+ use Doctrine \ORM \EntityManagerInterface ;
16
17
use Exception ;
17
18
use Symfony \Bundle \FrameworkBundle \Controller \AbstractController ;
18
19
use Symfony \Component \HttpFoundation \Request ;
@@ -37,7 +38,7 @@ class DatatableController extends AbstractController
37
38
*
38
39
* @return Response
39
40
*/
40
- public function editAction (Request $ request)
41
+ public function editAction (Request $ request, EntityManagerInterface $ entityManager ): Response
41
42
{
42
43
if ($ request ->isXmlHttpRequest ()) {
43
44
// x-editable sends some default parameters
@@ -57,7 +58,7 @@ public function editAction(Request $request)
57
58
}
58
59
59
60
// get an object by its primary key
60
- $ entity = $ this ->getEntityByPk ($ entityClassName , $ pk );
61
+ $ entity = $ this ->getEntityByPk ($ entityClassName , $ pk, $ entityManager );
61
62
62
63
/** @var PropertyAccessor $accessor */
63
64
/** @noinspection PhpUndefinedMethodInspection */
@@ -73,9 +74,8 @@ public function editAction(Request $request)
73
74
null !== $ path ? $ accessor ->setValue ($ entity , $ path , $ value ) : $ accessor ->setValue ($ entity , $ field , $ value );
74
75
75
76
// save all
76
- $ em = $ this ->getDoctrine ()->getManager ();
77
- $ em ->persist ($ entity );
78
- $ em ->flush ();
77
+ $ entityManager ->persist ($ entity );
78
+ $ entityManager ->flush ();
79
79
80
80
return new Response ('Success ' , 200 );
81
81
}
@@ -92,11 +92,9 @@ public function editAction(Request $request)
92
92
*
93
93
* @param string $entityClassName
94
94
*/
95
- private function getEntityByPk ($ entityClassName , $ pk ): object
95
+ private function getEntityByPk ($ entityClassName , $ pk, EntityManagerInterface $ entityManager ): object
96
96
{
97
- $ em = $ this ->getDoctrine ()->getManager ();
98
-
99
- $ entity = $ em ->getRepository ($ entityClassName )->find ($ pk );
97
+ $ entity = $ entityManager ->getRepository ($ entityClassName )->find ($ pk );
100
98
if (! $ entity ) {
101
99
throw $ this ->createNotFoundException ('DatatableController::getEntityByPk(): The entity does not exist. ' );
102
100
}
0 commit comments