Inject ManagerRegistry directly instead of getting from container

This commit is contained in:
Julie Lenaerts 2025-05-27 15:19:15 +02:00
parent 8ab03473b7
commit ce63506a1a
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);
}

View File

@ -24,6 +24,7 @@ use Chill\MainBundle\Pagination\PaginatorInterface;
use Chill\MainBundle\Repository\UserRepository;
use Chill\MainBundle\Security\ChillSecurity;
use Chill\MainBundle\Templating\Listing\FilterOrderHelper;
use Doctrine\Persistence\ManagerRegistry;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
@ -48,7 +49,7 @@ class UserController extends CRUDController
protected ParameterBagInterface $parameterBag,
private readonly TranslatorInterface $translator,
private readonly ChillSecurity $security,
private readonly \Doctrine\Persistence\ManagerRegistry $managerRegistry,
private readonly ManagerRegistry $managerRegistry,
) {}
#[Route(path: '/{_locale}/admin/main/user/{uid}/add_link_groupcenter', name: 'admin_user_add_groupcenter')]
@ -203,7 +204,7 @@ class UserController extends CRUDController
* Displays a form to edit the user current location.
*/
#[Route(path: '/{_locale}/main/user/current-location/edit', name: 'chill_main_user_currentlocation_edit')]
public function editCurrentLocationAction(Request $request): \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
public function editCurrentLocationAction(Request $request): RedirectResponse|Response
{
$user = $this->security->getUser();
$form = $this->createForm(UserCurrentLocationType::class, $user)
@ -234,7 +235,7 @@ class UserController extends CRUDController
* Displays a form to edit the user password.
*/
#[Route(path: '/{_locale}/admin/user/{id}/edit_password', name: 'admin_user_edit_password')]
public function editPasswordAction(User $user, Request $request): \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
public function editPasswordAction(User $user, Request $request): RedirectResponse|Response
{
$editForm = $this->createEditPasswordForm($user);
$editForm->handleRequest($request);