2017-03-02 63 views
0

我正在使用Symfony 2.8和FosUserBundle。 我有問題,我不能添加新的屬性到我的實體。 我的實體用戶看起來像將列添加到FosUserBundle表和表格Symfony 2.8

<?php 
// src/AppBundle/Entity/User.php 

namespace AppBundle\Entity; 

use FOS\UserBundle\Entity\User as BaseUser; 
use Doctrine\ORM\Mapping as ORM; 

/** 
* @ORM\Entity 
* @ORM\Table(name="fos_user") 
*/ 
class User extends BaseUser 
{ 
    /** 
    * @ORM\Id 
    * @ORM\Column(type="integer") 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    protected $id; 

    /** 
    * @ORM\name 
    * @ORM\Column(type="VARCHAR") 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    protected $name; 

    /** 
    * @ORM\lastname 
    * @ORM\Column(type="VARCHAR") 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    protected $lastname; 

    public function __construct() 
    { 
     parent::__construct(); 
     // your own logic 
    } 

    public function setName($name) 
    { 
     $this->name = $name; 

     return $this; 
    } 

    public function setLastname($lastname) 
    { 
     $this->lastname = $lastname; 

     return $this; 
    } 
} 

但我總是得到錯誤: 「AnnotationException在AnnotationException.php線54: [語義錯誤]註釋‘物業@Doctrine \ ORM \製圖\用戶名’ AppBundle \ Entity \ User :: $ name不存在,或者無法自動加載。「

也許我的RegistrationFormType有幫助,因爲我也改變了它。

<?php 

/* 
* This file is part of the FOSUserBundle package. 
* 
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> 
* 
* For the full copyright and license information, please view the LICENSE 
* file that was distributed with this source code. 
*/ 

namespace FOS\UserBundle\Form\Type; 

use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilderInterface; 
use Symfony\Component\OptionsResolver\OptionsResolverInterface; 

class RegistrationFormType extends AbstractType 
{ 
    private $class; 

    /** 
    * @param string $class The User class name 
    */ 
    public function __construct($class) 
    { 
     $this->class = $class; 
    } 

    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder 
      ->add('username', null, array('label' => 'form.username', 'translation_domain' => 'FOSUserBundle')) 
      ->add('email', 'email', array('label' => 'form.email', 'translation_domain' => 'FOSUserBundle')) 
      ->add('plainPassword', 'repeated', array(
       'type' => 'password', 
       'options' => array('translation_domain' => 'FOSUserBundle'), 
       'first_options' => array('label' => 'form.password'), 
       'second_options' => array('label' => 'form.password_confirmation'), 
       'invalid_message' => 'fos_user.password.mismatch', 
      )) 
      ->add('name') 
      ->add('lastname') 
     ; 
    } 

    public function setDefaultOptions(OptionsResolverInterface $resolver) 
    { 
     $resolver->setDefaults(array(
      'data_class' => $this->class, 
      'intention' => 'registration', 
     )); 
    } 

    public function getName() 
    { 
     return 'fos_user_registration'; 
    } 
} 

我在MySQL中手動添加了兩個字段名稱和姓氏。

哪些文件我忘了編輯?

最好的問候, 托馬斯

回答

0

我想你應該更正如下: 實體定義

namespace AppBundle\Entity; 

use FOS\UserBundle\Entity\User as BaseUser; 
use Doctrine\ORM\Mapping as ORM; 

/** 
* @ORM\Entity 
* @ORM\Table(name="fos_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") 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    protected $name; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="lastname",type="string") 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    protected $lastname; 

    public function __construct() 
    { 
     parent::__construct(); 
     // your own logic 
    } 

    public function setName($name) 
    { 
     $this->name = $name; 

     return $this; 
    } 

     public function getName() 
    { 
     return $this->name; 
    } 

    public function setLastname($lastname) 
    { 
     $this->lastname = $lastname; 

     return $this; 
    } 
     public function getLastname() 
    { 


     return $this->lastname ; 
    } 

} 

而且你不需要編輯FOSUserbundle FormType,而在你捆綁創建一個新的FormType基於您的用戶類。