Symfony2.7 - Call a service into a formhandler return Fatal error: Call to a member function on null

32 views Asked by At

I have a symfony 2.7 website that I have to add some functionalities to.

The last developers used formHandler in order to manage forms and keep controllers as clean as possible (that works fine). However, I need to call my service/manager in one of this formHandle but I always get the following error:

Fatal error: Call to a member function updateRole() on null.

There is no issue if I call my service in the controller...

I created my manager:

use Doctrine\ORM\EntityManager;
use Symfony\Bundle\FrameworkBundle\Translation\Translator;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Session;
use AgenceMAP\Famous\FirmsBundle\Entity\Commercial;
use Symfony\Component\Routing\Router;

class CommercialManager
{
    protected $em;
    protected $request;
    protected $session;
    protected $famous_parameters;
    protected $translator;
    protected $router;
    protected $kernel_root_dir;

    public function __construct(EntityManager $em, Request $request, Session $session,  Translator $translator, Router $router, $kernel_root_dir,$famous_parameters)
    {
        $this->em = $em;
        $this->request=$request;
        $this->session = $session;
        $this->translator=$translator;
        $this->router=$router;
        $this->kernel_root_dir= $kernel_root_dir;
        $this->famous_parameters=$famous_parameters;
    }

    public function updateRole($role, $commercial){
        var_dump($actualRole=$commercial->getRole());
        var_dump('In update role : '.$role);
        return "OK";
    }
}

Then I declared it as service:

#MANAGER commercial:
    famous.commercial_manager:
        class: AgenceMAP\Famous\MainBundle\Manager\CommercialManager
        arguments: ['@doctrine.orm.entity_manager',"@request",'@session','@translator.default','@router','%kernel.root_dir%','%famous%']
        scope: request

And finally I try to call it in the formHandler:

use Doctrine\ORM\EntityManager;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Request;
use AgenceMAP\Famous\EventsBundle\Entity\Speaker;

use AgenceMAP\Famous\ParticipationsBundle\Entity\Participant;
use AgenceMAP\Famous\FirmsBundle\Entity\Employee;
use AgenceMAP\Famous\FirmsBundle\Entity\Commercial;

class EmployeeHandler
{

    protected $request;
    protected $form;
    protected $em;

    public function __construct(FormInterface $form, Request $request,EntityManager $em)
    {
        $this->form = $form;
        $this->request = $request;
        $this->em = $em;

    }

    /**
     * @param boolean $confirmation
     */
    public function process($object=null)
    {
      /*some code*/
      $this->request->get('famous.commercial_manager')->updateRole($function, $commercial);
      /*some code*/
    }

I really don't understand why I got an error...

1

There are 1 answers

0
Xutyr On

Well I found the answer. I had to inject my service as argument of the FormHandler service like that:

famous.employee.form.handler:
        class: AgenceMAP\Famous\FirmsBundle\Form\Handler\EmployeeHandler
        scope: request
        arguments: ["@famous.employee.form", "@request","@doctrine.orm.entity_manager","@famous.commercial_manager"]

And then initialize it into the FormHandler constructor:

class EmployeeHandler
{

    protected $request;
    protected $form;
    protected $em;
    protected $commercialServices;

    public function __construct(FormInterface $form, Request $request,EntityManager $em,CommercialManager $commercialServices)
    {
        $this->form = $form;
        $this->request = $request;
        $this->em = $em;
        $this->commercialServices= $commercialServices;
    }
/*....*/
}

And now I can call my service like this:

$this->commercialServices->updateRole($function, $commercial);