2

我在服务中使用原则时遇到问题:

致命错误:在第 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'));
    }
}
4

1 回答 1

2

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

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

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

此外,您不必扩展ContainerAware,因为无论如何您都在注入您需要的服务。这意味着你不需要这个:

calls:
    - [ setContainer, [ @service_container ] ]
于 2012-10-04T14:51:03.633 回答