Answer the question
In order to leave comments, you need to log in
Why in Symfony i getDoctrine()->getManager() returns "Call to a member function has() on null"?
There is a ConfigManager class, which lies in src/Services/ConfigManager.php and inherits AbstractController
In the constructor of this class, I am trying to do the following
$this->entityManager = $this->getDoctrine()->getManager();
Call to a member function has() on null
Error:
Call to a member function has() on null
at vendor/symfony/framework-bundle/Controller/AbstractController.php:345
at Symfony\Bundle\FrameworkBundle\Controller\AbstractController->getDoctrine()
(src/Services/ConfigManager.php:39)
at App\Services\ConfigManager->__construct(object(Logger))
(src/Services/сrmAPI.php:33)
at App\Services\сrmAPI->__construct()
(src/Services/FormHandler.php:24)
at App\Services\FormHandler->registerModuleHandler(array(), object(Logger))
(src/Controller/FormsController.php:33)
at App\Controller\FormsController->index(object(Request))
(vendor/symfony/http-kernel/HttpKernel.php:157)
at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), 1)
(vendor/symfony/http-kernel/HttpKernel.php:79)
at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), 1, true)
(vendor/symfony/http-kernel/Kernel.php:196)
at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
(public/index.php:28)
Answer the question
In order to leave comments, you need to log in
In the config/services.yaml file, you can see that the controllers from the src/Controllers folder are included separately, which allows them to receive all the arguments automatically.
In your case, you create a service, but inherit the controller class (but you can’t just take it ...) If you just want to access the entityManager in your service, you need to remove the AbstractController, and write:
public function __construct(EntityManagerInterface $entityManager)
{
$this->entityManager = $entityManager;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question