Symfony - 登录用户没有填写所有字段

Symfony - logged user does not have all fields filled up

我正在使用 Symfony 2.8 和 Doctrine,我将 security.yml 配置为指向实现 UserInterface 的 User class 。 我的 ORM 架构在 YAML 中。

问题: - 在数据库中,用户还指定了 "email" 字段,LOGGED USER 的 Symfony 未填写该字段,而且 "password" 字段为空。

screenshot

当我做 $this->get('doctrine.orm.entity_manager')->clear(User::class); 然后实体管理器正在正确获取实体。 但是当实体是COMING FROM SESSION时,那就不正确了。

问题还在于,当我尝试在存储库上使用 find() 从数据库中获取新实体时,当我不使用 $em->clear(User::class)

问题: - 如何告诉 Symfony/Doctrine 以填充所有字段的方式构建我的实体,以便它变得持久?

<?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;
    }
}

和ORM配置:

XXX\AppBundle\Model\Entity\User:
    type: entity
    table: users
    repositoryClass: XXX\AppBundle\Model\Repository\UserRepository
    id:
        id:
            type: integer
            scale: 0
            length: null
            unique: false
            nullable: false
            precision: 0
            id: true
            generator:
                strategy: IDENTITY
    fields:
        username:
            type: string
            scale: 0
            length: null
            unique: true
            nullable: false
            precision: 0
        email:
            type: string
            scale: 0
            length: null
            unique: true
            nullable: false
            precision: 0
        password:
            type: string
            scale: 0
            length: null
            unique: false
            nullable: false
            precision: 0
        roles:
            type: json_array
            scale: 0
            length: null
            unique: false
            nullable: false
            precision: 0
    oneToMany:
        logEvents:
            targetEntity: XXX\AppBundle\Model\Entity\Server\Logging
            cascade:
                - remove
                - persist
            fetch: LAZY
            mappedBy: author
            inversedBy: null
            orphanRemoval: false
            orderBy: null
    lifecycleCallbacks: {  }

感谢您的宝贵时间。 祝你有美好的一天:-)

您以取消设置电子邮件和密码的方式实现了 UserInterface::eraseCredentials() 方法:

public function eraseCredentials()
{
    $this->password = null;
    $this->email    = null;
}

Symfony 将在序列化对象之前调用此方法以将其保存在会话中。

UserInterface::eraseCredentials() 旨在从用户对象中删除敏感数据,因此它不会最终出现在会话文件中,但实际上没有必要像您一样删除电子邮件正在做。一个更好的例子是,如果您将用户密码的纯文本版本存储在此对象的某处,这是您永远不想在会话文件中结束的东西。