dsm13698679318 2012-10-04 14:48
浏览 70

在自定义服务symfony2中插入doctrine

I have a problem using doctrine into the service :

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

the code the service is:

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

the code the listener is:

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条回答 默认 最新

  • doucuo4413 2012-10-04 14:51
    关注

    You can have more than one parameters in a constructor:

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

    But you can't have several constructors in a class and __constructor is not a constructor method name, so you should remove that method.

    Also, you don't have to extend ContainerAware because you're injecting the services you need anyway. It means you don't need this:

    calls:
        - [ setContainer, [ @service_container ] ]
    
    评论

报告相同问题?

悬赏问题

  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算