【问题标题】:insert doctrine in custom service symfony2在自定义服务 symfony2 中插入原则
【发布时间】:2012-09-25 15:32:48
【问题描述】:

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

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

【问题讨论】:

    标签: php symfony login persist


    【解决方案1】:

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

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

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

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

    calls:
        - [ setContainer, [ @service_container ] ]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-27
      相关资源
      最近更新 更多