Inject ManagerRegistry directly instead of getting from container

This commit is contained in:
2025-05-27 15:19:15 +02:00
parent c08c909545
commit d9ce4e3ec6
4 changed files with 20 additions and 27 deletions

View File

@@ -39,6 +39,8 @@ abstract class AbstractCRUDController extends AbstractController
*/
protected array $crudConfig = [];
public function __construct(protected ManagerRegistry $doctrine) {}
/**
* get the role given from the config.
*/
@@ -62,7 +64,6 @@ abstract class AbstractCRUDController extends AbstractController
return \array_merge(
parent::getSubscribedServices(),
[
'doctrine' => ManagerRegistry::class,
'chill_main.paginator_factory' => PaginatorFactory::class,
'translator' => TranslatorInterface::class,
AuthorizationHelper::class => AuthorizationHelper::class,
@@ -98,7 +99,7 @@ abstract class AbstractCRUDController extends AbstractController
*/
protected function buildQueryEntities(string $action, Request $request)
{
$qb = $this->getManagerRegistry()->getManager()
$qb = $this->doctrine->getManager()
->createQueryBuilder()
->select('e')
->from($this->getEntityClass(), 'e');
@@ -170,8 +171,7 @@ abstract class AbstractCRUDController extends AbstractController
*/
protected function getEntity(mixed $action, string $id, Request $request): object
{
$e = $this->getManagerRegistry()
->getRepository($this->getEntityClass())
$e = $this->doctrine->getRepository($this->getEntityClass())
->find($id);
if (null === $e) {
@@ -181,7 +181,7 @@ abstract class AbstractCRUDController extends AbstractController
$expectedVersion = $request->query->getInt('entity_version');
try {
$manager = $this->getManagerRegistry()->getManagerForClass($this->getEntityClass());
$manager = $this->doctrine->getManagerForClass($this->getEntityClass());
if ($manager instanceof EntityManagerInterface) {
$manager->lock($e, LockMode::OPTIMISTIC, $expectedVersion);
@@ -211,11 +211,6 @@ abstract class AbstractCRUDController extends AbstractController
return $this->container->get('chill_main.paginator_factory');
}
protected function getManagerRegistry(): ManagerRegistry
{
return $this->container->get('doctrine');
}
/**
* Get the result of the query.
*/

View File

@@ -80,8 +80,8 @@ class ApiController extends AbstractCRUDController
return $response;
}
$this->getManagerRegistry()->getManager()->remove($entity);
$this->getManagerRegistry()->getManager()->flush();
$this->doctrine->getManager()->remove($entity);
$this->doctrine->getManager()->flush();
$response = $this->onAfterFlush($action, $request, $_format, $entity, $errors);

View File

@@ -62,6 +62,8 @@ class CRUDController extends AbstractController
*/
protected array $crudConfig;
public function __construct(private readonly ManagerRegistry $managerRegistry) {}
public function CRUD(?string $parameter): Response
{
return new Response($parameter);
@@ -175,7 +177,7 @@ class CRUDController extends AbstractController
*/
protected function buildQueryEntities(string $action, Request $request)
{
$query = $this->getManagerRegistry()
$query = $this->managerRegistry
->getManager()
->createQueryBuilder()
->select('e')
@@ -289,7 +291,7 @@ class CRUDController extends AbstractController
if ($form->isSubmitted() && $form->isValid()) {
$this->onFormValid($action, $entity, $form, $request);
$em = $this->getManagerRegistry()->getManager();
$em = $this->managerRegistry->getManager();
$this->onPreRemove($action, $entity, $form, $request);
$this->removeEntity($action, $entity, $form, $request);
@@ -334,7 +336,7 @@ class CRUDController extends AbstractController
$id = $request->query->get('duplicate_id', 0);
$originalEntity = $this->getEntity($action, $id, $request);
$this->getManagerRegistry()->getManager()
$this->managerRegistry->getManager()
->detach($originalEntity);
return $originalEntity;
@@ -406,7 +408,7 @@ class CRUDController extends AbstractController
if ($form->isSubmitted() && $form->isValid()) {
$this->onFormValid($action, $entity, $form, $request);
$em = $this->getManagerRegistry()->getManager();
$em = $this->managerRegistry->getManager();
$this->onPrePersist($action, $entity, $form, $request);
$em->persist($entity);
@@ -509,7 +511,7 @@ class CRUDController extends AbstractController
if ($form->isSubmitted() && $form->isValid()) {
$this->onFormValid($action, $entity, $form, $request);
$em = $this->getManagerRegistry()->getManager();
$em = $this->managerRegistry->getManager();
$this->onPreFlush($action, $entity, $form, $request);
$em->flush();
@@ -623,7 +625,7 @@ class CRUDController extends AbstractController
*/
protected function getEntity(mixed $action, $id, Request $request): ?object
{
return $this->getManagerRegistry()
return $this->managerRegistry
->getRepository($this->getEntityClass())
->find($id);
}
@@ -672,11 +674,6 @@ class CRUDController extends AbstractController
return $this->container->get('chill_main.paginator_factory');
}
protected function getManagerRegistry(): ManagerRegistry
{
return $this->container->get(ManagerRegistry::class);
}
/**
* Get the result of the query.
*/
@@ -946,7 +943,7 @@ class CRUDController extends AbstractController
protected function removeEntity(string $action, $entity, FormInterface $form, Request $request)
{
$this->getManagerRegistry()
$this->managerRegistry
->getManager()
->remove($entity);
}