Symfony2-可捕获的致命错误:_construct()必须是条令\ORM\EntityManager的实例
我正在构建“上次登录”功能。创建activitylistener并将侦听器定义为服务后,我得到一个错误 ContextErrorException:可捕获的致命错误:传递给Acme\SecurityBundle\EventListener\ActivityListener:的参数2::\uu construct()必须是条令\ORM\EntityManager的实例,给定条令\Bundle\DoctrineBundle\Registry的实例,在第350行的C:\xampp\htdocs\sinfoc\public\app\cache\dev\appDevDebugProjectContainer.php中调用,并在C:\xampp\htdocs\sinfoc\public\src\Acme\SecurityBundle\EventListener\ActivityListener.php第22行中定义 这是我的ActivityListener类Symfony2-可捕获的致命错误:_construct()必须是条令\ORM\EntityManager的实例,symfony,orm,doctrine,entitymanager,event-listener,Symfony,Orm,Doctrine,Entitymanager,Event Listener,我正在构建“上次登录”功能。创建activitylistener并将侦听器定义为服务后,我得到一个错误 ContextErrorException:可捕获的致命错误:传递给Acme\SecurityBundle\EventListener\ActivityListener:的参数2::\uu construct()必须是条令\ORM\EntityManager的实例,给定条令\Bundle\DoctrineBundle\Registry的实例,在第350行的C:\xampp\htdocs\sin
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);
}
}
}
}
来自此错误的
ContextErrorException:可捕获的致命错误:传递给Acme\SecurityBundle\EventListener\ActivityListener:的参数2::\uu construct()必须是条令\ORM\EntityManager的实例,给定条令\Bundle\DoctrineBundle\Registry的实例,在第350行的C:\xampp\htdocs\sinfoc\public\app\cache\dev\appDevDebugProjectContainer.php中调用,并在C:\xampp\htdocs\sinfoc\public\src\Acme\SecurityBundle\EventListener\ActivityListener.php第22行中定义
你要提供
条令\ORM\EntityManager
但是你提供了
条令\Bundle\DoctrineBundle\Registry
在
你的论点应该是正确的
arguments:
- "@security.context"
- "@doctrine.orm.entity_manager"
您没有错过错误消息的一部分吗?你能在ActivityListener.php中显示类声明吗?作为Patt建议的继续,我没有使用FOSUserBundle。我在注入@doctor.orm.entity时出现此错误_manager@PéCé刚刚添加了整个错误消息和完整的类声明刷新您的缓存,看起来ActivityListener接收到的是“@doctor”,而不是“@doctor.orm.entity_manager”parameter@P塞西清除了缓存。但错误依然存在。
arguments:
- "@security.context"
- "@doctrine.orm.entity_manager"