我正在與Symfony聯繫,這是我的第一個應用程序,我試圖瞭解所有環境。這是一個非常簡單的問題,但經過網絡漫長的衝浪之後,我沒有得到如何去做。在Symfony 3登錄後更新用戶數據
我有一個登錄表單,它非常適合處理成功或錯誤的偵聽器,所有工作都很好,但是當我嘗試更新用戶的上次訪問日期時出現問題。
我有一個用戶實體(從文檔獲取):
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
/**
* @ORM\Table(name="app_users")
* @ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
*/
class User implements AdvancedUserInterface, \Serializable
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="string", length=25, unique=true)
*/
private $username;
/**
* @ORM\Column(type="string", length=64)
*/
private $password;
/**
* @ORM\Column(type="string", length=60, unique=true)
*/
private $email;
/**
* @ORM\Column(name="is_active", type="boolean")
*/
private $isActive;
/**
* @ORM\Column(name="salt", type="string", length=100)
*/
private $salt;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $lastLogin = null;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $createdAt = null;
public function __construct()
{
$this->isActive = true;
// may not be needed, see section on salt below
// $this->salt = md5(uniqid(null, true));
}
public function getUsername()
{
return $this->username;
}
public function setSalt($salt)
{
$this->salt = $salt;
}
public function getSalt()
{
// you *may* need a real salt depending on your encoder
// see section on salt below
return null;
}
public function getPassword()
{
return $this->password;
}
public function getRoles()
{
return array('ROLE_USER');
}
public function eraseCredentials()
{
}
public function isAccountNonExpired()
{
return true;
}
public function isAccountNonLocked()
{
return true;
}
public function isCredentialsNonExpired()
{
return true;
}
public function isEnabled()
{
return $this->isActive;
}
/** @see \Serializable::serialize() */
public function serialize()
{
return serialize(array(
$this->id,
$this->username,
$this->password,
$this->isActive,
$this->createdAt,
$this->lastLogin,
// see section on salt below
// $this->salt,
));
}
/** @see \Serializable::unserialize() */
public function unserialize($serialized)
{
list (
$this->id,
$this->username,
$this->password,
$this->isActive,
$this->createdAt,
$this->lastLogin,
// see section on salt below
// $this->salt
) = unserialize($serialized);
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set username
*
* @param string $username
*
* @return User
*/
public function setUsername($username)
{
$this->username = $username;
return $this;
}
/**
* Set password
*
* @param string $password
*
* @return User
*/
public function setPassword($password)
{
$this->password = $password;
return $this;
}
/**
* Set email
*
* @param string $email
*
* @return User
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set isActive
*
* @param boolean $isActive
*
* @return User
*/
public function setIsActive($isActive)
{
$this->isActive = $isActive;
return $this;
}
/**
* Get isActive
*
* @return boolean
*/
public function getIsActive()
{
return $this->isActive;
}
/**
* Updates last user login date
*
*/
public function updateLastLogin()
{
$this->lastAccess = new \DateTime("now");
}
public function getLastLogin()
{
return $this->lastLogin->format('Y-m-d H:i:s');
}
public function getCreatedAt()
{
return $this->createdAt->format('Y-m-d H:i:s');
}
}
當在我的用戶登錄調用updateLastLogin
方法:
<?php
namespace AppBundle\EventListener;
use Symfony\Component\Security\Core\Event\AuthenticationEvent;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
class LoginListener {
public function __construct(){
}
public function onSuccessfulLogin(AuthenticationEvent $event)
{
$user = $event->getAuthenticationToken()->getUser();
if($user instanceof AdvancedUserInterface){
$user->updateLastLogin();
}
}
public function onLoginError(AuthenticationEvent $event)
{
// Login error
}
}
時,達到updateLastLogin
代碼,但它不」 t在數據庫中得到更新。誰能告訴我我做錯了什麼?
預先感謝您
的@mdma的回答使我對正確的解決方案,與他的解決方案我有一個Call to a member function getManager() on string
錯誤。爲了解決它,我只是在監聽器構造注入EntityManager
並添加缺少的呼叫在數據庫中持久化數據:
<?php
namespace AppBundle\EventListener;
use Doctrine\ORM\EntityManager;
use Symfony\Component\Security\Core\Event\AuthenticationEvent;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
class LoginListener {
protected $entityManager;
public function __construct(EntityManager $entityManager){
$this->entityManager = $entityManager;
}
public function onSuccessfulLogin(AuthenticationEvent $event)
{
$user = $event->getAuthenticationToken()->getUser();
if($user instanceof AdvancedUserInterface){
$user->updateLastLogin();
$this->entityManager->persist($user);
$this->entityManager->flush();
}
}
public function onLoginError(AuthenticationEvent $event)
{
// Login error
}
}
而在services.yml
文件我增加一條,作爲參數:
app.login_service:
class: AppBundle\EventListener\LoginListener
arguments: ["@doctrine.orm.entity_manager"]
tags:
- { name: kernel.event_listener, event: security.authentication.success, method: onSuccessfulLogin }
現在它按預期工作。
注意在更新登錄日期時,用戶類中存在拼寫錯誤,我正在使用一個不存在的變量。而不是$this->lastAccess = new \DateTime("now");
它必須是:
$this->lastLogin = new \DateTime("now");
大問題,很好的解釋!爲什麼有人會投票否定? – mburakergenc