我是symfony2的新用户,我安装了fosuserbundle并在注册表单中添加了自定义字段。表单已正确提交,但自定义字段数据未保存在数据库中。
RegistrationFormType.php
<?php
namespace madhur\DemoBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
class RegistrationFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
// add your custom field
$builder->add('name');
}
public function getParent()
{
return 'fos_user_registration';
}
public function getName()
{
return 'madhur_user_registration';
}
}
User.php实体
namespace madhur\DemoBundle\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* User
*/
class User extends BaseUser
{
public function __construct()
{
parent::__construct();
// your own logic
}
/**
* @ORM\Column(type="string", length=255)
*
* @Assert\NotBlank(message="Please enter your name.", groups={"Registration", "Profile"})
* @Assert\Length(
* min=3,
* max="255",
* minMessage="The name is too short.",
* maxMessage="The name is too long.",
* groups={"Registration", "Profile"}
* )
*/
protected $name;
public function getName() {
return $this->name;
}
public function setName($name) {
$this->name= $name;
}
}
config.yml
fos_user:
db_driver: orm # other valid values are 'mongodb', 'couchdb' and 'propel'
firewall_name: main
user_class: madhur\DemoBundle\Entity\User
registration:
form:
type: madhur_user_registration
service.yml
madhur_user.registration.form.type:
class: madhur\DemoBundle\Form\Type\RegistrationFormType
tags:
- { name: form.type, alias: madhur_user_registration }
User.orm.yml
madhur\DemoBundle\Entity\User:
type: entity
table: fos_user
id:
id:
type: integer
generator:
strategy: AUTO
fields:
name:
type: string
length: 255
请帮助!!
答案 0 :(得分:1)
在这么晚的时候,我的代码中没有看到任何错误。 您是否更新了数据库架构并清除了缓存?
app / console doctrine:schema:update --force
app / console cache:clear
app / console cache:clear --env = prod