我正在使用Symfony 2.8和Doctrine,我将security.yml配置为指向实现 UserInterface 的用户类。 我的ORM架构在YAML中。
问题: - 在数据库中,用户还指定了“email”字段,LOGGED USER的Symfony没有填写该字段,“password”字段也是空的。
当我 public static TreeNode convert_loop(char[] expr) {
if (expr == null || expr.length < 1) {
return null;
}
if (expr.length == 1) {
return new TreeNode(expr[0]);
}
if ((expr.length - 1) % 4 != 0) {
throw new InputMismatchException("wrong expression");
}
int start = 0, end = expr.length - 1;
TreeNode<Character> root = new TreeNode<>(expr[start]);
root.right = new TreeNode<>(expr[end]);
start += 2;
end -= 2;
TreeNode<Character> cur = root;
while (start != end) {
TreeNode<Character> node = new TreeNode<>(expr[start]);
node.right = new TreeNode<>(expr[end]);
cur.left = node;
cur = node;
start += 2;
end -= 2;
}
cur.left = new TreeNode(expr[start]);// care
return root;
}
时
那么实体管理器正确地获取实体。
但是当实体正在来自会话时,则它是不正确的。
问题还在于,当我尝试使用存储库上的find()从数据库中获取新的实体时,当我不使用$this->get('doctrine.orm.entity_manager')->clear(User::class);
问题: - 如何告诉Symfony / Doctrine以这样的方式构建我的实体,它将填满所有字段,以便它可以保持不变?
$em->clear(User::class)
和ORM配置:
<?php
namespace XXX\AppBundle\Model\Entity;
use XXX\AppBundle\Model\Entity\Server\Logging;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* User
*/
class User implements UserInterface
{
/**
* @var integer $id
*/
protected $id;
/**
* @var string $username
*/
protected $username;
/**
* @var string $email
*/
protected $email;
/**
* @var string $password
*/
protected $password;
/**
* @var array $roles
*/
protected $roles = array();
/**
* @var \Doctrine\Common\Collections\Collection $logEvents
*/
protected $logEvents;
/**
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* {@inheritdoc}
*/
public function getUsername()
{
return $this->username;
}
/**
* @param string $username
* @return $this
*/
public function setUsername($username)
{
$this->username = $username;
return $this;
}
/**
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* @param string $email
* @return $this
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* {@inheritdoc}
*/
public function getPassword()
{
return $this->password;
}
/**
* @param string $password
* @return $this
*/
public function setPassword($password)
{
$this->password = $password;
return $this;
}
/**
* Returns the roles or permissions granted to the user for security.
*/
public function getRoles()
{
$roles = $this->roles;
// guarantees that a user always has at least one role for security
if (empty($roles)) {
$roles[] = 'ROLE_USER';
}
return array_unique($roles);
}
public function setRoles(array $roles)
{
$this->roles = $roles;
}
/**
* Returns the salt that was originally used to encode the password.
*/
public function getSalt()
{
// See "Do you need to use a Salt?" at http://symfony.com/doc/current/cookbook/security/entity_provider.html
// we're using bcrypt in security.yml to encode the password, so
// the salt value is built-in and you don't have to generate one
return;
}
/**
* Removes sensitive data from the user.
*/
public function eraseCredentials()
{
$this->password = null;
$this->email = null;
}
/**
* Appends an entry to administration log
*
* @param \XXX\AppBundle\Model\Entity\Server\Logging $logEvent
* @return $this
*/
public function appendLog(Server\Logging $logEvent)
{
if (!$this->logEvents->contains($logEvent))
{
$this->logEvents->add($logEvent);
}
return $this;
}
/**
* Remove a log entry from the history
*
* @param \XXX\AppBundle\Model\Entity\Server\Logging $logEvent
* @return $this
*/
public function clearLogEntry(Server\Logging $logEvent)
{
$this->logEvents->removeElement($logEvent);
return $this;
}
}
感谢您的时间。 祝你有个美好的一天: - )
答案 0 :(得分:1)
您实现了UserInterface :: eraseCredentials()方法,其方式是取消设置电子邮件和密码:
public function eraseCredentials()
{
$this->password = null;
$this->email = null;
}
Symfony将在序列化对象之前调用此方法,以将其保存在会话中。
UserInterface :: eraseCredentials()用于从用户对象中删除敏感数据,因此它不会以例如会话文件结束,但实际上并不需要像您一样删除电子邮件。一个更好的例子是,如果你在这个对象的某个地方存储用户密码的纯文本版本,这是你永远不想在会话文件中结束的东西。