代码之家  ›  专栏  ›  技术社区  ›  Gary Henshall

无法使用TWIG和Symfony 2、FOSUserBundle从app.user的用户表中检索所有字段

  •  1
  • Gary Henshall  · 技术社区  · 9 年前

    我目前正在制作一个网站,我有一个实体User,它有几个字段。但当我试图检索登录的当前用户时,它只检索用户名。

    UserType中的字段如下:

    public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder
                ->add('email')
                ->add('name')
                ->add('building')
                ->add('room')
            ;
        }
    

    我使用这个html代码和TWIG来检索字段:

    <table class="table table-striped">
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Building</th>
                    <th>Room Number</th>
                    <th>Email</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>{{ app.user.name }}</td>
                    <td>{{ app.user.building }}</td>
                    <td>{{ app.user.room }}</td>
                    <td>{{ app.user.email }}</td>
                </tr>
            </tbody>
        </table>
    

    但当我在浏览器中查看表时,我只会在表中填充电子邮件字段?

    用户实体:

    <?php
    
    namespace FYProject\ProjectBundle\Entity;
    
    use FOS\UserBundle\Model\User as BaseUser;
    use Doctrine\ORM\Mapping as ORM;
    
    /**
     *
     * @ORM\Entity
     * @ORM\Table(name="fyp_user")
     */
    
    class User extends BaseUser
    {
        /**
         * @ORM\Id
         * @ORM\Column(type="integer")
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        protected $id;
    
        /**
         * @var string
         */
        protected $username;
    
        /**
         * @var string
         */
        protected $password;
    
        /**
         * @var string
         */
        protected $name;
    
        /**
         * @var string
         */
        protected $lastname;
    
        /**
         * @var string
         */
        protected $school;
    
    
        /**
         * @var string
         */
        protected $qualification;
    
        /**
         * @var string
         */
        protected $modeofstudy;
    
        /**
         * @var string
         */
        protected $programmecode;
    
        /**
         * @var integer
         */
        protected $programmeyear;
    
        /**
         * @var datetime
         */
        protected $startdate;
    
        /**
         * @var string
         */
        protected $building;
    
        /**
         * @var integer
         */
        protected $room;
    
    
        /**
         * Get id
         *
         * @return integer 
         */
        public function getId()
        {
            return $this->id;
        }
    
        /**
         * Set username
         *
         * @param string $username
         * @return Entity
         */
        public function setUsername($username)
        {
            $this->username = $username;
    
            return $this;
        }
    
        /**
         * Get username
         *
         * @return string 
         */
        public function getUsername()
        {
            return $this->username;
        }
    
        /**
         * Set password
         *
         * @param string $password
         * @return Entity
         */
        public function setPassword($password)
        {
            $this->password = $password;
    
            return $this;
        }
    
        /**
         * Get password
         *
         * @return string 
         */
        public function getPassword()
        {
            return $this->password;
        }
    
        /**
         * Set name
         *
         * @param string $name
         * @return Entity
         */
        public function setName($name)
        {
            $this->name = $name;
    
            return $this;
        }
    
        /**
         * Get name
         *
         * @return string 
         */
        public function getName()
        {
            return $this->name;
        }
    
    
        /**
         * Set school
         *
         * @param string $school
         * @return Entity
         */
        public function setSchool($school)
        {
            $this->school = $school;
    
            return $this;
        }
    
        /**
         * Get school
         *
         * @return string
         */
        public function getSchool()
        {
            return $this->school;
        }
    
        /**
         * set qualification
         *
         * @param string qualification
         * @return Entity
         */
        public function setQualification($qualification){
    
            $this->qualification = $qualification;
            return $this;
        }
    
        /**
         * get qualification
         *
         * @return string
         */
        public function getQualification(){
    
            return $this->qualification;
        }
    
        /**
         * set modeofstudy
         *
         * @param string modeofstudy
         * @return Entity
         */
        public function setModeofstudy($modeofstudy){
    
            $this->modeofstudy = $modeofstudy;
            return $this;
        }
    
        /**
         * get modeofstudy
         *
         * @return string
         */
        public function getModeofstudy(){
    
            return $this->modeofstudy;
        }
    
        /**
         * set programmecode
         *
         * @param string programmecode
         * @return Entity
         */
        public function setProgrammecode($programmecode){
    
            $this->programmecode = $programmecode;
            return $this;
        }
    
        /**
         * get programmecode
         *
         * @return string
         */
        public function getProgrammecode(){
    
            return $this->programmecode;
        }
    
        /**
         * set programmeyear
         *
         * @param integer programmeyearn
         * @return Entity
         */
        public function setProgrammeyear($programmeyear){
    
            $this->programmecode = $programmeyear;
            return $this;
        }
    
        /**
         * get programmecode
         *
         * @return integer
         */
        public function getProgrammeyear(){
    
            return $this->programmeyear;
        }
    
        /**
         * set startdate
         *
         * @param datetime startdate
         * @return Entity
         */
        public function setStartdate($startdate){
    
            $this->startdate = $startdate;
            return $this;
        }
    
        /**
         * get startdate
         *
         * @return datetime
         */
        public function getStartdate(){
    
            return $this->startdate;
        }
    
        /**
         * set lastname
         *
         * @param string lastname
         * @return Entity
         */
        public function setLastname($lastname){
    
            $this->lastname = $lastname;
            return $this;
        }
    
        /**
         * get lastname
         *
         * @return string
         */
        public function getLastname(){
    
            return $this->lastname;
        }
    
        /**
         * set building
         * @param string building
         * @return Entity
         */
        public function setBuilding($building){
    
            $this->building = $building;
            return $this;
        }
    
        /**
         * get building
         *
         * @return string
         */
        public function getBuilding(){
    
            return $this->building;
        }
    
        /**
         * set room
         * @param integer room
         * @return Entity
         */
        public function setRoom($room){
    
            $this->room = $room;
            return $this;
        }
    
        /**
         * get room
         *
         * @return integer
         */
        public function getRoom(){
    
            return $this->room;
        }
    
        public function __construct(){
    
            parent::__construct();
        }
    }
    

    注册当前字段时,使用dump(app.user)返回null:

     User {#306 ▼
      #id: 9
      #name: null
      #lastname: null
      #school: null
      #qualification: null
      #modeofstudy: null
      #programmecode: null
      #programmeyear: null
      #startdate: null
      #building: null
      #room: null
    
    2 回复  |  直到 9 年前
        1
  •  0
  •   Najd developper    9 年前

    使用此文档摘要覆盖表单类型 https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/overriding_forms.md

    更新数据库架构

    清除缓存

        2
  •  0
  •   Gary Henshall    9 年前

    问题已解决

    我没有为实体用户中的每个字段设置@ORM\列。更新的用户实体:

        <?php
    
    namespace FYProject\ProjectBundle\Entity;
    
    use FOS\UserBundle\Model\User as BaseUser;
    use Doctrine\ORM\Mapping as ORM;
    
    /**
     *
     * @ORM\Entity
     * @ORM\Table(name="fyp_user")
     */
    
    class User extends BaseUser
    {
        /**
         * @ORM\Id
         * @ORM\Column(type="integer")
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        protected $id;
    
    
        /**
         * @var string
         *
         * @ORM\Column(name="name", type="string", length=100)
         *
         */
    
        protected $name;
    
        /**
         * @var string
         *
         * @ORM\Column(name="lastname", type="string", length=100)
         */
        protected $lastname;
    
        /**
         * @var string
         *
         * @ORM\Column(name="school", type="string", length=100)
         */
        protected $school;
    
    
        /**
         * @var string
         *
         * @ORM\Column(name="qualification", type="string", length=50)
         */
        protected $qualification;
    
        /**
         * @var string
         *
         * @ORM\Column(name="modeofstudy", type="string", length=50)
         */
        protected $modeofstudy;
    
        /**
         * @var string
         *
         * @ORM\Column(name="programmecode", type="string", length=50)
         */
        protected $programmecode;
    
        /**
         * @var integer
         *
         * @ORM\Column(name="programmeyear", type="integer", length=10)
         */
        protected $programmeyear;
    
        /**
         * @var datetime
         *
         * @ORM\Column(name="startdate", type="datetime", length=50)
         */
        protected $startdate;
    
        /**
         * @var string
         *
         * @ORM\Column(name="building", type="string", length=50)
         */
        protected $building;
    
        /**
         * @var integer
         *
         * @ORM\Column(name="room", type="integer", length=50)
         */
        protected $room;
    
    
        /**
         * Get id
         *
         * @return integer 
         */
        public function getId()
        {
            return $this->id;
        }
    
    
    
        /**
         * Set name
         *
         * @param string $name
         * @return Entity
         */
        public function setName($name)
        {
            $this->name = $name;
    
            return $this;
        }
    
        /**
         * Get name
         *
         * @return string 
         */
        public function getName()
        {
            return $this->name;
        }
    
    
        /**
         * Set school
         *
         * @param string $school
         * @return Entity
         */
        public function setSchool($school)
        {
            $this->school = $school;
    
            return $this;
        }
    
        /**
         * Get school
         *
         * @return string
         */
        public function getSchool()
        {
            return $this->school;
        }
    
        /**
         * set qualification
         *
         * @param string $qualification
         * @return Entity
         */
        public function setQualification($qualification){
    
            $this->qualification = $qualification;
            return $this;
        }
    
        /**
         * get qualification
         *
         * @return string
         */
        public function getQualification(){
    
            return $this->qualification;
        }
    
        /**
         * set modeofstudy
         *
         * @param string $modeofstudy
         * @return Entity
         */
        public function setModeofstudy($modeofstudy){
    
            $this->modeofstudy = $modeofstudy;
            return $this;
        }
    
        /**
         * get modeofstudy
         *
         * @return string
         */
        public function getModeofstudy(){
    
            return $this->modeofstudy;
        }
    
        /**
         * set programmecode
         *
         * @param string $programmecode
         * @return Entity
         */
        public function setProgrammecode($programmecode){
    
            $this->programmecode = $programmecode;
            return $this;
        }
    
        /**
         * get programmecode
         *
         * @return string
         */
        public function getProgrammecode(){
    
            return $this->programmecode;
        }
    
        /**
         * set programmeyear
         *
         * @param integer $programmeyear
         * @return Entity
         */
        public function setProgrammeyear($programmeyear){
    
            $this->programmecode = $programmeyear;
            return $this;
        }
    
        /**
         * get programmecode
         *
         * @return integer
         */
        public function getProgrammeyear(){
    
            return $this->programmeyear;
        }
    
        /**
         * set startdate
         *
         * @param datetime $startdate
         * @return Entity
         */
        public function setStartdate($startdate){
    
            $this->startdate = $startdate;
            return $this;
        }
    
        /**
         * get startdate
         *
         * @return datetime
         */
        public function getStartdate(){
    
            return $this->startdate;
        }
    
        /**
         * set lastname
         *
         * @param string $lastname
         * @return Entity
         */
        public function setLastname($lastname){
    
            $this->lastname = $lastname;
            return $this;
        }
    
        /**
         * get lastname
         *
         * @return string
         */
        public function getLastname(){
    
            return $this->lastname;
        }
    
        /**
         * set building
         * @param string $building
         * @return Entity
         */
        public function setBuilding($building){
    
            $this->building = $building;
            return $this;
        }
    
        /**
         * get building
         *
         * @return string
         */
        public function getBuilding(){
    
            return $this->building;
        }
    
        /**
         * set room
         * @param integer $room
         * @return Entity
         */
        public function setRoom($room){
    
            $this->room = $room;
            return $this;
        }
    
        /**
         * get room
         *
         * @return integer
         */
        public function getRoom(){
    
            return $this->room;
        }
    
        public function __construct(){
    
            parent::__construct();
        }
    }
    

    问题已解决!谢谢你的帮助!