mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-20 22:53:49 +00:00
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:
@@ -31,6 +31,9 @@ abstract class AbstractCRUDController extends AbstractController
|
||||
* This configuration si defined by `chill_main['crud']` or `chill_main['apis']`
|
||||
*/
|
||||
protected array $crudConfig = [];
|
||||
public function __construct(private readonly \Doctrine\Persistence\ManagerRegistry $managerRegistry)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* get the role given from the config.
|
||||
@@ -91,7 +94,7 @@ abstract class AbstractCRUDController extends AbstractController
|
||||
*/
|
||||
protected function buildQueryEntities(string $action, Request $request)
|
||||
{
|
||||
$qb = $this->getDoctrine()->getManager()
|
||||
$qb = $this->managerRegistry->getManager()
|
||||
->createQueryBuilder()
|
||||
->select('e')
|
||||
->from($this->getEntityClass(), 'e');
|
||||
@@ -163,8 +166,7 @@ abstract class AbstractCRUDController extends AbstractController
|
||||
*/
|
||||
protected function getEntity(mixed $action, string $id, Request $request): object
|
||||
{
|
||||
$e = $this
|
||||
->getDoctrine()
|
||||
$e = $this->managerRegistry
|
||||
->getRepository($this->getEntityClass())
|
||||
->find($id);
|
||||
|
||||
|
@@ -23,6 +23,9 @@ use Symfony\Component\Validator\ConstraintViolationListInterface;
|
||||
|
||||
class ApiController extends AbstractCRUDController
|
||||
{
|
||||
public function __construct(private readonly \Doctrine\Persistence\ManagerRegistry $managerRegistry)
|
||||
{
|
||||
}
|
||||
/**
|
||||
* Base method for handling api action.
|
||||
*
|
||||
@@ -80,8 +83,8 @@ class ApiController extends AbstractCRUDController
|
||||
return $response;
|
||||
}
|
||||
|
||||
$this->getDoctrine()->getManager()->remove($entity);
|
||||
$this->getDoctrine()->getManager()->flush();
|
||||
$this->managerRegistry->getManager()->remove($entity);
|
||||
$this->managerRegistry->getManager()->flush();
|
||||
|
||||
$response = $this->onAfterFlush($action, $request, $_format, $entity, $errors);
|
||||
|
||||
@@ -153,7 +156,7 @@ class ApiController extends AbstractCRUDController
|
||||
return $response;
|
||||
}
|
||||
|
||||
$this->getDoctrine()->getManager()->flush();
|
||||
$this->managerRegistry->getManager()->flush();
|
||||
|
||||
$response = $this->onAfterFlush($action, $request, $_format, $entity, $errors);
|
||||
|
||||
@@ -269,10 +272,10 @@ class ApiController extends AbstractCRUDController
|
||||
}
|
||||
|
||||
if ($forcePersist && Request::METHOD_POST === $request->getMethod()) {
|
||||
$this->getDoctrine()->getManager()->persist($postedData);
|
||||
$this->managerRegistry->getManager()->persist($postedData);
|
||||
}
|
||||
|
||||
$this->getDoctrine()->getManager()->flush();
|
||||
$this->managerRegistry->getManager()->flush();
|
||||
|
||||
$response = $this->onAfterFlush($action, $request, $_format, $entity, $errors, [$postedData]);
|
||||
|
||||
@@ -403,8 +406,8 @@ class ApiController extends AbstractCRUDController
|
||||
return $response;
|
||||
}
|
||||
|
||||
$this->getDoctrine()->getManager()->persist($entity);
|
||||
$this->getDoctrine()->getManager()->flush();
|
||||
$this->managerRegistry->getManager()->persist($entity);
|
||||
$this->managerRegistry->getManager()->flush();
|
||||
|
||||
$response = $this->onAfterFlush($action, $request, $_format, $entity, $errors);
|
||||
|
||||
|
@@ -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);
|
||||
}
|
||||
|
Reference in New Issue
Block a user