2012-10-04 31 views
2

我使用学说到服务有一个问题:插入主义的Symfony2

Fatal error: Call to a member function persist() on a non-object in /var/www/Symfony/src/mio/mioBundle/AuthenticationHandler.php on line 37

代码中的服务是:

services: 
    authentication_handler: 
     class: mio\mioBundle\AuthenticationHandler 
     arguments: [@router , @doctrine.orm.entity_manager ] 
     calls: 
      - [ setContainer, [ @service_container ] ] 

代码听者:

class AuthenticationHandler extends ContainerAware implements AuthenticationSuccessHandlerInterface{ 

    protected $router; 

    protected $em; 

     public function __construct(RouterInterface $router) 
    { 
     $this->router = $router; 
    } 


    public function __constructor(EntityManager $entityManager) 
    { 
     $this->em = $entityManager; 
    } 

    public function onAuthenticationSuccess(Request $request, TokenInterface $token) 
    { 
     $empleado = $token->getUser(); 
     $empleado->setNombre("abeeeer"); 
     $this->em->persist($empleado); //line 37 
     $this->em->flush(); 

     //return new Response($token->getUsername()); 
     return new RedirectResponse($this->router->generate('familia')); 
    } 
} 

回答

2

您可以在构造函数中有多个参数:

public function __construct(RouterInterface $router, EntityManager $em) 
{ 
    $this->router = $router; 
    $this->em = $em; 
} 

但是,你不能在一个类中有几个构造函数,并且__constructor不是一个构造函数方法名,所以你应该删除该方法。

此外,您无需扩展ContainerAware,因为您正在注入所需的服务。这意味着你不需要这个:

calls: 
    - [ setContainer, [ @service_container ] ]