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

@@ -25,7 +25,7 @@ use Symfony\Contracts\Translation\TranslatorInterface;
*/
class CustomFieldController extends AbstractController
{
public function __construct(private readonly TranslatorInterface $translator) {}
public function __construct(private readonly TranslatorInterface $translator, private readonly \Doctrine\Persistence\ManagerRegistry $managerRegistry) {}
/**
* Creates a new CustomField entity.
@@ -39,7 +39,7 @@ class CustomFieldController extends AbstractController
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em = $this->managerRegistry->getManager();
$em->persist($entity);
$em->flush();
@@ -65,7 +65,7 @@ class CustomFieldController extends AbstractController
*/
public function editAction(mixed $id)
{
$em = $this->getDoctrine()->getManager();
$em = $this->managerRegistry->getManager();
$entity = $em->getRepository(CustomField::class)->find($id);
@@ -94,7 +94,7 @@ class CustomFieldController extends AbstractController
$cfGroupId = $request->query->get('customFieldsGroup', null);
if (null !== $cfGroupId) {
$cfGroup = $this->getDoctrine()->getManager()
$cfGroup = $this->managerRegistry->getManager()
->getRepository(CustomFieldsGroup::class)
->find($cfGroupId);
@@ -119,7 +119,7 @@ class CustomFieldController extends AbstractController
*/
public function updateAction(Request $request, mixed $id)
{
$em = $this->getDoctrine()->getManager();
$em = $this->managerRegistry->getManager();
$entity = $em->getRepository(\Chill\CustomFieldsBundle\Entity\CustomField::class)->find($id);

View File

@@ -36,7 +36,8 @@ class CustomFieldsGroupController extends AbstractController
/**
* CustomFieldsGroupController constructor.
*/
public function __construct(private readonly CustomFieldProvider $customFieldProvider, private readonly TranslatorInterface $translator) {}
public function __construct(
private readonly CustomFieldProvider $customFieldProvider, private readonly TranslatorInterface $translator, private readonly \Doctrine\Persistence\ManagerRegistry $managerRegistry) {}
/**
* Creates a new CustomFieldsGroup entity.
@@ -50,7 +51,7 @@ class CustomFieldsGroupController extends AbstractController
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em = $this->managerRegistry->getManager();
$em->persist($entity);
$em->flush();
@@ -76,7 +77,7 @@ class CustomFieldsGroupController extends AbstractController
*/
public function editAction(mixed $id)
{
$em = $this->getDoctrine()->getManager();
$em = $this->managerRegistry->getManager();
$entity = $em->getRepository(\Chill\CustomFieldsBundle\Entity\CustomFieldsGroup::class)->find($id);
@@ -99,7 +100,7 @@ class CustomFieldsGroupController extends AbstractController
*/
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$em = $this->managerRegistry->getManager();
$cfGroups = $em->getRepository(\Chill\CustomFieldsBundle\Entity\CustomFieldsGroup::class)->findAll();
$defaultGroups = $this->getDefaultGroupsId();
@@ -131,7 +132,7 @@ class CustomFieldsGroupController extends AbstractController
$cFGroupId = $form->get('id')->getData();
$em = $this->getDoctrine()->getManager();
$em = $this->managerRegistry->getManager();
$cFGroup = $em->getRepository(\Chill\CustomFieldsBundle\Entity\CustomFieldsGroup::class)->findOneById($cFGroupId);
@@ -192,7 +193,7 @@ class CustomFieldsGroupController extends AbstractController
*/
public function renderFormAction($id, Request $request)
{
$em = $this->getDoctrine()->getManager();
$em = $this->managerRegistry->getManager();
$entity = $em->getRepository(\Chill\CustomFieldsBundle\Entity\CustomFieldsGroup::class)->find($id);
@@ -236,7 +237,7 @@ class CustomFieldsGroupController extends AbstractController
*/
public function showAction(mixed $id)
{
$em = $this->getDoctrine()->getManager();
$em = $this->managerRegistry->getManager();
$entity = $em->getRepository(\Chill\CustomFieldsBundle\Entity\CustomFieldsGroup::class)->find($id);
@@ -260,7 +261,7 @@ class CustomFieldsGroupController extends AbstractController
*/
public function updateAction(Request $request, mixed $id)
{
$em = $this->getDoctrine()->getManager();
$em = $this->managerRegistry->getManager();
$entity = $em->getRepository(\Chill\CustomFieldsBundle\Entity\CustomFieldsGroup::class)->find($id);
@@ -313,7 +314,7 @@ class CustomFieldsGroupController extends AbstractController
->add('submit', SubmitType::class);
$builder->get('customFieldsGroup')
->addViewTransformer(new CustomFieldsGroupToIdTransformer(
$this->getDoctrine()->getManager()
$this->managerRegistry->getManager()
));
return $builder->getForm();
@@ -380,7 +381,7 @@ class CustomFieldsGroupController extends AbstractController
*/
private function getDefaultGroupsId()
{
$em = $this->getDoctrine()->getManager();
$em = $this->managerRegistry->getManager();
$customFieldsGroupIds = $em->createQuery('SELECT g.id FROM '
.CustomFieldsDefaultGroup::class.' d '