Symfony我怎么能为我的FOSUser添加字段?

时间:2016-03-26 15:05:22

标签: symfony fosuserbundle

我正在尝试向用户添加字段“名称”,这就是我继续进行的方式

 namespace User\UserBundle\Controller;


use FOS\UserBundle\Controller\RegistrationController as BaseController;
class DefaultController extends BaseController
{
    public function indexAction()
    {
        $response = parent::registerAction();


        // ... do custom stuff
        return $response;
    }

这是我的userType

 public function buildForm(FormBuilderInterface $builder, array $options)
    {
        parent::buildForm($builder, $options);
        $builder
            ->add('nom')
            ->add('prenom')
        ;
    } 

当我尝试添加时,

{{ form_widget(form.name) }}

我收到此错误

Method "nom" for object "Symfony\Component\Form\FormView" does not exist in FOSUserBundle:Registration:register.html.twig at line
      This is my user class 

namespace User\UserBundle\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;

    public function __construct()
    {
        parent::__construct();
        // your own logic
    }
    /**
     * @var string
     *
     * @ORM\Column(name="nom", type="string", length=255, nullable=true )
     */
    private $nom;
     /**
     * @var string
     *
     * @ORM\Column(name="prenom", type="string", length=255, nullable=true )
     */
    private $prenom;



    /**
     * Set nom
     *
     * @param string $nom
     * @return User
     */
    public function setNom($nom)
    {
        $this->nom = $nom;

        return $this;
    }

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

    /**
     * Set prenom
     *
     * @param string $prenom
     * @return User
     */
    public function setPrenom($prenom)
    {
        $this->prenom = $prenom;

        return $this;
    }

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

这就是我在app / config下的内容 enter image description here
 我该怎么办?有什么建议吗?我是FOSUserBundle的新手。感谢

4 个答案:

答案 0 :(得分:0)

您似乎正在尝试将nom和prenom字段添加到注册表单中。因此,您需要覆盖FOSUserBundle注册表单。按照Overriding Default FOSUserBundle Forms上的指南进行操作,该指南显示如何覆盖任何表单。

答案 1 :(得分:0)

答案 2 :(得分:0)

  

这是我的config.yml

你是在config.yml中写的吗?

您需要在config.yml中编写此代码:

fos_user:
db_driver: orm 
firewall_name: main
user_class: User\UserBundle\Entity\User
registration:
    form:
        type: user_userbundle_user

,其余写入./config/services.yml

services:
fos_user.doctrine_registry:
    alias: doctrine
app.form.registration:
    class: UserUserBundle\Form\UserType
    tags:
        - { name: form.type, alias: user_userbundle_user }

答案 3 :(得分:0)

所以我终于明白了!我将构造添加到了我的userType

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

这是我的新服务.yml

parameters:
    user_user.registration.class: User\UserBundle\Entity\User

services:
    user.form.user:
        class: User\UserBundle\Form\UserType
        arguments: [%user_user.registration.class%]
        tags:
            - { name: form.type, alias: user_userbundle_user }

我希望它能帮助someOne。谢谢大家