2012-10-04 43 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 ] ]