S
S
Sergey Chazov2020-07-17 20:01:35
symfony
Sergey Chazov, 2020-07-17 20:01:35

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();

what I get
Call to a member function has() on null

Here is the trace:
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)


I got acquainted with Symphony a week ago, so, if possible, explain in detail: what am I doing wrong.
I realized that I don't have some kind of container.. but where can I get it and how does it work?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Olga Svistunova, 2020-07-17
@chazovs

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 question

Ask a Question

731 491 924 answers to any question