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

@@ -38,7 +38,8 @@ class AccompanyingPeriodController extends AbstractController
protected AccompanyingPeriodACLAwareRepositoryInterface $accompanyingPeriodACLAwareRepository,
private readonly EventDispatcherInterface $eventDispatcher,
private readonly ValidatorInterface $validator,
private readonly TranslatorInterface $translator
private readonly TranslatorInterface $translator,
private readonly \Doctrine\Persistence\ManagerRegistry $managerRegistry
) {}
/**
@@ -86,7 +87,7 @@ class AccompanyingPeriodController extends AbstractController
'%name%' => $person->__toString(),
]));
$this->getDoctrine()->getManager()->flush();
$this->managerRegistry->getManager()->flush();
return $this->redirectToRoute('chill_person_accompanying_period_list', [
'person_id' => $person->getId(),
@@ -159,7 +160,7 @@ class AccompanyingPeriodController extends AbstractController
$form->isValid(['Default', 'closed'])
&& 0 === \count($errors)
) {
$em = $this->getDoctrine()->getManager();
$em = $this->managerRegistry->getManager();
$em->persist($accompanyingPeriod);
$em->flush();
$flashBag->add(
@@ -271,7 +272,7 @@ class AccompanyingPeriodController extends AbstractController
['%name%' => $person->__toString()]
));
$this->getDoctrine()->getManager()->flush();
$this->managerRegistry->getManager()->flush();
return $this->redirectToRoute('chill_person_accompanying_period_list', [
'person_id' => $person->getId(),
@@ -327,7 +328,7 @@ class AccompanyingPeriodController extends AbstractController
$this->_validatePerson($person);
$this->getDoctrine()->getManager()->flush();
$this->managerRegistry->getManager()->flush();
$this->addFlash('success', $this->translator->trans(
'The period has been re-opened'
@@ -357,7 +358,7 @@ class AccompanyingPeriodController extends AbstractController
*/
public function updateAction(int $person_id, int $period_id, Request $request): Response
{
$em = $this->getDoctrine()->getManager();
$em = $this->managerRegistry->getManager();
/** @var AccompanyingPeriod $accompanyingPeriod */
$accompanyingPeriod = $em->getRepository(AccompanyingPeriod::class)->find($period_id);
@@ -430,7 +431,7 @@ class AccompanyingPeriodController extends AbstractController
*/
private function _getPerson(int $id): Person
{
$person = $this->getDoctrine()->getManager()
$person = $this->managerRegistry->getManager()
->getRepository(\Chill\PersonBundle\Entity\Person::class)->find($id);
if (null === $person) {