我有一个 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*/
}
我真的不明白为什么我得到一个错误......