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

@@ -37,6 +37,9 @@ class CRUDController extends AbstractController
* This configuration si defined by `chill_main['crud']`.
*/
protected array $crudConfig;
public function __construct(private readonly \Doctrine\Persistence\ManagerRegistry $managerRegistry)
{
}
public function CRUD(?string $parameter): Response
{
@@ -150,8 +153,7 @@ class CRUDController extends AbstractController
*/
protected function buildQueryEntities(string $action, Request $request)
{
$query = $this
->getDoctrine()
$query = $this->managerRegistry
->getManager()
->createQueryBuilder()
->select('e')
@@ -265,7 +267,7 @@ class CRUDController extends AbstractController
if ($form->isSubmitted() && $form->isValid()) {
$this->onFormValid($action, $entity, $form, $request);
$em = $this->getDoctrine()->getManager();
$em = $this->managerRegistry->getManager();
$this->onPreRemove($action, $entity, $form, $request);
$this->removeEntity($action, $entity, $form, $request);
@@ -310,7 +312,7 @@ class CRUDController extends AbstractController
$id = $request->query->get('duplicate_id', 0);
$originalEntity = $this->getEntity($action, $id, $request);
$this->getDoctrine()->getManager()
$this->managerRegistry->getManager()
->detach($originalEntity);
return $originalEntity;
@@ -382,7 +384,7 @@ class CRUDController extends AbstractController
if ($form->isSubmitted() && $form->isValid()) {
$this->onFormValid($action, $entity, $form, $request);
$em = $this->getDoctrine()->getManager();
$em = $this->managerRegistry->getManager();
$this->onPrePersist($action, $entity, $form, $request);
$em->persist($entity);
@@ -485,7 +487,7 @@ class CRUDController extends AbstractController
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();
@@ -599,7 +601,7 @@ class CRUDController extends AbstractController
*/
protected function getEntity(mixed $action, $id, Request $request): ?object
{
return $this->getDoctrine()
return $this->managerRegistry
->getRepository($this->getEntityClass())
->find($id);
}
@@ -918,7 +920,7 @@ class CRUDController extends AbstractController
protected function removeEntity(string $action, $entity, FormInterface $form, Request $request)
{
$this->getDoctrine()
$this->managerRegistry
->getManager()
->remove($entity);
}