0

我有一个 symfony 2.7 网站,我必须添加一些功能。

最后一个开发人员用于formHandler管理表单并保持控制器尽可能干净(工作正常)。但是,我需要在其中之一中调用我的服务/经理,formHandle但我总是收到以下错误:

致命错误:在 null 上调用成员函数 updateRole()。

如果我在控制器中调用我的服务没有问题......

我创建了我的经理:

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";
    }
}

然后我将其声明为服务:

#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

最后我尝试在以下位置调用它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*/
    }

我真的不明白为什么我得到一个错误......

4

1 回答 1

0

好吧,我找到了答案。我不得不像这样将我的服务作为 FormHandler 服务的参数注入:

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"]

然后将其初始化到 FormHandler 构造函数中:

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;
    }
/*....*/
}

现在我可以这样调用我的服务:

$this->commercialServices->updateRole($function, $commercial);
于 2019-08-15T09:10:19.053 回答