2014-02-24 45 views
2

我正在构建'Last Logged In'功能。创建activitylistener和定义监听的服务后,我得到一个错误Symfony2 - 可捕获的致命错误:_construct()必须是Doctrine ORM EntityManager的实例

ContextErrorException: Catchable Fatal Error: Argument 2 passed to Acme\SecurityBundle\EventListener\ActivityListener::__construct() must be an instance of Doctrine\ORM\EntityManager, instance of Doctrine\Bundle\DoctrineBundle\Registry given, called in C:\xampp\htdocs\sinfoc\public\app\cache\dev\appDevDebugProjectContainer.php on line 350 and defined in C:\xampp\htdocs\sinfoc\public\src\Acme\SecurityBundle\EventListener\ActivityListener.php line 22

这里是我的ActivityListener类

public function __construct(SecurityContext $securityContext, EntityManager $entityManager) 
     { 
      $this->securityContext = $securityContext; 
      $this->entityManager = $entityManager; 
     } 

而且这里是听众被定义为服务services.yml

services: 
    activity_listener: 
     class: Bnpp\SecurityBundle\EventListener\ActivityListener 
     arguments: [@security.context, @doctrine.orm.entity_manager] 
     tags: 
     - { name: kernel.event_listener, event: kernel.controller, method: onCoreController } 

下面是完整的ActivityListenerClass

<?php 

namespace Bnpp\SecurityBundle\EventListener; 

use Doctrine\ORM\EntityManager; 
//use Symfony\Component\Security\Core\User\UserInterface; 
use Symfony\Component\Security\Core\SecurityContext; 
use Symfony\Component\HttpKernel\Event\FilterControllerEvent; 
use Symfony\Component\HttpKernel\HttpKernel; 
use Bnpp\SecurityBundle\Entity\User; 
use Doctrine\Bundle\DoctrineBundle\Registry; 


/** 
* Listener that updates the last activity of the authenticated user 
*/ 

class ActivityListener 

    { 
    protected $securityContext; 
    protected $entityManager; 

    public function __construct(SecurityContext $securityContext, EntityManager $entityManager) 
    { 
     $this->securityContext = $securityContext; 
     $this->entityManager = $entityManager; 
    } 



    /** 
    * Update the user "lastActivity" on each request 
    * @param FilterControllerEvent $event 
    */ 


    public function onCoreController(FilterControllerEvent $event) 
    { 

     // Check that the current request is a "MASTER_REQUEST" 
     // Ignore any sub-request 
     if ($event->getRequestType() !== HttpKernel::MASTER_REQUEST) { 
      return; 
     } 

     // Check token authentication availability 
     if ($this->securityContext->getToken()) { 
      $user = $this->securityContext->getToken()->getUser(); 


      if (($user instanceof User) && !($user->isActiveNow())) { 
       $user->setLastActivity(new \DateTime('now')); 
       $this->entityManager->flush($user); 
      } 
     } 

    } 

} 
+0

您是否错过了错误信息的一部分?你可以在ActivityListener.php中显示类声明吗? – AlterPHP

+0

为了继续推荐Patt,我没有使用FOSUserBundle。在注入@ doctrine.orm.entity_manager时出现此错误 – CloudJedi

+0

@PéCé刚刚添加了整个错误消息并完成了类声明 – CloudJedi

回答

0

从这个错误

ContextErrorException: Catchable Fatal Error: Argument 2 passed to Acme\SecurityBundle\EventListener\ActivityListener::__construct() must be an instance of Doctrine\ORM\EntityManager, instance of Doctrine\Bundle\DoctrineBundle\Registry given, called in C:\xampp\htdocs\sinfoc\public\app\cache\dev\appDevDebugProjectContainer.php on line 350 and defined in C:\xampp\htdocs\sinfoc\public\src\Acme\SecurityBundle\EventListener\ActivityListener.php line 22

你提供

Doctrine\ORM\EntityManager,

,但你在

arguments: [@security.context, @doctrine.orm.entity_manager] 

提供

Doctrine\Bundle\DoctrineBundle\Registry

您的论据应该是

arguments: 
     - "@security.context" 
     - "@doctrine.orm.entity_manager" 
相关问题