我正在使用Symfony 2.8和FosUserBundle。 我有问题,我无法向我的实体添加新属性。 我的实体用户看起来像那样
<?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.php第54行中的AnnotationException: [语义错误]属性AppBundle \ Entity \ User :: $ name中的注释“@Doctrine \ ORM \ Mapping \ 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 :(得分: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。