,您也可以尝试使用称为
我愿意构建登录名的听众,更新登录计数,并在用户成功登录时更新登录日期到数据库。
我成功地获得了用户详细信息,但无法访问它们,也无法更新数据库
src/appbundle/listerer/securityListener.php:
namespace AppBundleListener;
use SymfonyComponentSecurityHttpEventInteractiveLoginEvent;
use SymfonyComponentSecurityCoreAuthenticationTokenStorageTokenStorage;
#use DoctrineBundleDoctrineBundleRegistry as Doctrine;
use DoctrineORMEntityManager;
class SecurityListener
{
private $tokenStorage;
public function __construct(TokenStorage $tokenStorage, EntityManager $doctrine)
{
}
public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
{
if ( $event->getAuthenticationToken()->isAuthenticated() ) {
$user = $event->getAuthenticationToken()->getUser();
var_dump($user->id);
}
/**
* Update lastLogin and loginCount for user in database
*/
$em = $this->getDoctrine()->getManager();
$user = $em->getRepository('AppBundle:User')->find($user->id);
if( !$user )
{
throw $this->createNotFoundException(
'User could not be found for id '. $user->id
);
}
$user->setLastLogin(date());
$user->setLoginCount($user->loginCount +1);
$em->flush;
}
}
错误:
Error: Cannot access private property AppBundleEntityUser::$id
实体属性通常是私有的,但是您始终可以使用getter方法。
请更新您的代码类似于此示例:
throw $this->createNotFoundException(
'User could not be found for id '. $user->getId()
);
如果我的示例失败。
getID()
的方法。