(Symfony3)如何向FOSUserBundle添加自定义属性并保留它?

时间:2017-03-17 11:37:59

标签: php doctrine-orm fosuserbundle symfony-3.2

我试图将自定义属性(psptCode)添加到我的用户实体,扩展UserBundle。 这是我的用户类:

<?php

namespace AppBundle\Entity;

use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use JMS\Serializer\Annotation as JMSSerializer;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;
use AppBundle\Model\UserInterface;

/**
 * @ORM\Entity
 * @ORM\Table(name="fos_user")
 *
 * @UniqueEntity("email")
 * @UniqueEntity("username")
 * @UniqueEntity("psptCode")
 * @JMSSerializer\ExclusionPolicy("all")
 * @JMSSerializer\AccessorOrder("custom", custom = {"id", "emailCanonical", "account"})
 */
class User extends BaseUser implements UserInterface
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     * @JMSSerializer\Expose
     * @JMSSerializer\Type("string")
     */
    protected $id;

    /**
     * @var string The canonical representation of the email of the user.
     *
     * @JMSSerializer\Expose
     * @JMSSerializer\Type("string")
     * @JMSSerializer\SerializedName("email")
     * @JMSSerializer\Groups({"user_all", "user_summary"})
     */
    protected $emailCanonical;

    /**
     * @var string
     *
     * @ORM\Column(type="string", name="pspt_code", length=16, nullable=true)
     */
    private $psptCode;

    /**
     * @var Account The Pspt account related to this user, if any
     *
     * @ORM\OneToOne(targetEntity="Account")
     * @ORM\JoinColumn(name="pspt_code", referencedColumnName="cli_id", nullable=true)
     * @JMSSerializer\Expose
     * @JMSSerializer\Type("AppBundle\Entity\Account")
     * @JMSSerializer\Groups({"user_all"})
     */
    protected $account;

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * @param $pspt_code
     *
     * Set the cli_id to retrieve the account, if any
     * @return User
     */
    public function setPsptCode($pspt_code)
    {
      $this->psptCode = $pspt_code;

      return $this;
    }

    /**
     * Get psptCode
     * @return string
     */
    public function getPsptCode()
    {
      return $this->psptCode;
    }
}

当我创建数据库模式时,我的自定义属性被创建,但是当我尝试将值存储到我的自定义属性中时,它不会持久存储到数据库中:

$user = $this->userManager->createUser();
$user->setEnabled(true);
$user->setUsername('peter');
$user->setEmail('peter@test.com');
$user->setPlainPassword('testpass');
$user->setPsptCode('XXXXXXXXXX');
$this->userManager->updateUser($user);

用户存储如下:

AppBundle\Entity\User {#4041
  #id: 1
  #emailCanonical: "peter@test.com"
  -psptCode: null
  #account: null
  #username: "peter"
  #usernameCanonical: "peter"
  #email: "peter@test.com"
  #enabled: true
  #salt: null
  #password: "$2y$15$OJ4ynZmpBaDYbK7N.d35m.FdcSHGpZG7Yemcwxrg4kHhKzIyP3XOO"
  #plainPassword: null
  #lastLogin: null
  #confirmationToken: null
  #passwordRequestedAt: null
  #groups: null
  #roles: []
}

因此,我无法存储用户的自定义属性。 请帮忙!

0 个答案:

没有答案