FOSUserBundle 扩展 RegisterForm

FOSUserBundle extend RegisterForm

我正在使用 Symfony 2。8.x 和 FOSUserBundle。

我尝试使用姓氏和名字等几个字段来扩展我的用户...我按照文档中的说明进行操作 https://symfony.com/doc/master/bundles/FOSUserBundle/overriding_forms.html

当我尝试注册时收到消息

Neither the property "name" nor one of the methods "getName()", "name()", "isName()", "hasName()", "__get()" exist and have public access in class "AppBundle\Entity\User"

这是我的注册Class

    <?php
namespace AppBundle\Form;

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

class RegistrationType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('name');
    }

    public function getParent()
    {
        return 'FOS\UserBundle\Form\Type\RegistrationFormType';

        // Or for Symfony < 2.8
        // return 'fos_user_registration';
    }

    public function getBlockPrefix()
    {
        return 'app_user_registration';
    }

    // For Symfony 2.x
    public function getName()
    {
        return $this->getBlockPrefix();
    }
}

这是我的用户 Class

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

namespace AppBundle\Entity;

use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity
 * @ORM\Table(name="fos_user")
 */
class User extends BaseUser
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;
    /**
     * @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;
    /**
     * @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 $firstname;
    /**
     * @ORM\Column(type="integer")
     */
    protected $mentorid;

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

有人可以帮我解决这个问题吗?

此致

您是否尝试为字段 'name' 添加 setter 和 getter? 使用例如 doctrine:generate:entities YourBundleName

你真的需要按照安德烈的建议去做。您需要为您的字段生成 setter 和 getter。您可以手动完成,但我会说从自动生成方法开始更安全,之后您可以根据需要自定义这些方法。你需要 运行

php app/console doctrine:generate:entities AppBundle/Entity/User

有关详细信息,请参阅 http://symfony.com/doc/current/doctrine.html#generating-getters-and-setters