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

@@ -23,6 +23,9 @@ use Symfony\Component\HttpFoundation\Response;
*/
class EntityPersonCRUDController extends CRUDController
{
public function __construct(private readonly \Doctrine\Persistence\ManagerRegistry $managerRegistry)
{
}
/**
* Override the base method to add a filtering step to a person.
*
@@ -102,7 +105,7 @@ class EntityPersonCRUDController extends CRUDController
return null;
}
$person = $this->getDoctrine()
$person = $this->managerRegistry
->getRepository(Person::class)
->find($request->query->getInt('person_id'));

View File

@@ -20,6 +20,9 @@ use Symfony\Component\HttpFoundation\Response;
class OneToOneEntityPersonCRUDController extends CRUDController
{
public function __construct(private readonly \Doctrine\Persistence\ManagerRegistry $managerRegistry)
{
}
protected function generateRedirectOnCreateRoute($action, Request $request, $entity): string
{
throw new \BadMethodCallException('Not implemented yet.');
@@ -31,7 +34,7 @@ class OneToOneEntityPersonCRUDController extends CRUDController
if (null === $entity) {
$entity = $this->createEntity($action, $request);
$person = $this->getDoctrine()
$person = $this->managerRegistry
->getManager()
->getRepository(Person::class)
->find($id);
@@ -58,7 +61,7 @@ class OneToOneEntityPersonCRUDController extends CRUDController
protected function onPostFetchEntity($action, Request $request, $entity): ?Response
{
if (false === $this->getDoctrine()->getManager()->contains($entity)) {
if (false === $this->managerRegistry->getManager()->contains($entity)) {
return new RedirectResponse($this->generateRedirectOnCreateRoute($action, $request, $entity));
}
@@ -67,6 +70,6 @@ class OneToOneEntityPersonCRUDController extends CRUDController
protected function onPreFlush(string $action, $entity, FormInterface $form, Request $request)
{
$this->getDoctrine()->getManager()->persist($entity);
$this->managerRegistry->getManager()->persist($entity);
}
}