FOSUserBundle扩展了RegisterForm

时间:2017-02-05 10:19:32

标签: symfony fosuserbundle

我正在使用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"

这是我的注册类

    <?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();
    }
}

这是我的用户类

<?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
    }
}

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

祝你好运

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

你真的需要做安德烈建议的事情。您需要为您的字段生成setter和getter。您可以手动执行此操作,但我认为从自动生成方法开始更安全,之后您可以根据需要进行自定义。你需要运行

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

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