Refactor code to directly use Doctrine's ManagerRegistry

Replaced most of the invocations of getDoctrine()->getManager() with ManagerRegistry->getManager(), and added ManagerRegistry injection to controllers where needed. This is part of an ongoing effort to improve code clarity, and avoid unnecessary method chaining in various parts of the codebase.
This commit is contained in:
2023-12-16 19:09:34 +01:00
parent 655dc02538
commit 5703fd0046
54 changed files with 281 additions and 228 deletions

View File

@@ -46,7 +46,8 @@ class UserController extends CRUDController
private readonly UserRepository $userRepository,
protected ParameterBagInterface $parameterBag,
private readonly TranslatorInterface $translator,
private readonly ChillSecurity $security
private readonly ChillSecurity $security,
private readonly \Doctrine\Persistence\ManagerRegistry $managerRegistry
) {}
/**
@@ -55,7 +56,7 @@ class UserController extends CRUDController
*/
public function addLinkGroupCenterAction(Request $request, mixed $uid): Response
{
$em = $this->getDoctrine()->getManager();
$em = $this->managerRegistry->getManager();
$user = $em->getRepository(\Chill\MainBundle\Entity\User::class)->find($uid);
@@ -107,7 +108,7 @@ class UserController extends CRUDController
*/
public function deleteLinkGroupCenterAction(mixed $uid, mixed $gcid, Request $request): RedirectResponse
{
$em = $this->getDoctrine()->getManager();
$em = $this->managerRegistry->getManager();
$user = $em->getRepository(\Chill\MainBundle\Entity\User::class)->find($uid);
@@ -165,7 +166,7 @@ class UserController extends CRUDController
if ($form->isSubmitted() && $form->isValid()) {
$this->onFormValid($action, $entity, $form, $request);
$em = $this->getDoctrine()->getManager();
$em = $this->managerRegistry->getManager();
$this->onPreFlush($action, $entity, $form, $request);
$em->flush();
@@ -216,7 +217,7 @@ class UserController extends CRUDController
$user->setCurrentLocation($currentLocation);
$this->getDoctrine()->getManager()->flush();
$this->managerRegistry->getManager()->flush();
$this->addFlash('success', $this->translator->trans('Current location successfully updated'));
return $this->redirect(
@@ -252,7 +253,7 @@ class UserController extends CRUDController
$user->setPassword($this->passwordEncoder->encodePassword($user, $password));
$this->getDoctrine()->getManager()->flush();
$this->managerRegistry->getManager()->flush();
$this->addFlash('success', $this->translator->trans('Password successfully updated!'));
return $this->redirect(
@@ -431,7 +432,7 @@ class UserController extends CRUDController
private function getPersistedGroupCenter(GroupCenter $groupCenter)
{
$em = $this->getDoctrine()->getManager();
$em = $this->managerRegistry->getManager();
$groupCenterManaged = $em->getRepository(\Chill\MainBundle\Entity\GroupCenter::class)
->findOneBy([