我的网站运行的是symfony 3.4,我建立了自己的用户成员系统。
我的用户实体包含一个日期时间字段“lastlogin”,我找不到每次用户登录时更新该字段的解决方案。
我创建了一个自定义用户检查器,然后尝试更新其中的字段:
<?php
namespace CoreBundle\Security;
use CoreBundle\Entity\User as AppUser;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\User\UserCheckerInterface;
use Symfony\Component\Security\Core\User\UserInterface;
class UserChecker implements UserCheckerInterface
{
public function checkPreAuth(UserInterface $user)
{
if (!$user instanceof AppUser) {
return;
}
if ( $user->getDeleted() || !$user->getEnabled() )
{
throw new AuthenticationException();
}
else
{
$entityManager = $this->get('doctrine')->getManager();
$user->setLastLogin(new \DateTime());
$entityManager->persist($user);
$entityManager->flush();
}
}
public function checkPostAuth(UserInterface $user)
{
if (!$user instanceof AppUser) {
return;
}
}
}
但它不起作用。也许我不能在这个文件中使用条令实体管理器?
如果我使用
$this->get('doctrine')->getManager();
我得到:
致命错误:调用未定义的方法
corebundle\security\userchecker::get()。