在自定义服务symfony2中插入doctrine

时间:2012-10-04 14:48:22

标签: php symfony login persist

我在使用doctrine进入服务时遇到了问题:

  

致命错误:在第37行的/var/www/Symfony/src/mio/mioBundle/AuthenticationHandler.php中的非对象上调用成员函数persist()

服务的代码是:

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'));
    }
}

1 个答案:

答案 0 :(得分:2)

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

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

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

另外,您不必延长ContainerAware,因为您无论如何都要注入所需的服务。这意味着你不需要这个:

calls:
    - [ setContainer, [ @service_container ] ]