Symfony - 属性 "first_name" 和其中一种方法都不存在并且在 class "Symfony\Component\Form\FormView" 中没有 public 访问权限

Symfony - Neither the property "first_name" nor one of the methods exist and have public access in class "Symfony\Component\Form\FormView"

我几个小时以来一直在阅读并寻找解决这个问题的方法。但似乎我无法真正修复此错误。我发现只有当我在 FOS/UserBundle/Form/Type/RegistrationFormType 中添加表单时,这是它在树枝中显示的唯一时间。我想我在调用 UserBundle/Form/RegistrationFormType 时遇到了一些问题。 在这方面急需帮助。

我正在使用 FOSUserBundle

这是我的 用户 class:

<?php

namespace UserBundle\Entity;

use FOS\UserBundle\Entity\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(name="first_name", type="string", length=255, nullable=false)
 * @Assert\NotBlank(message="Please enter your first name", groups={"Registration", "Profile"})
 */
protected $firstName;

/**
 * @ORM\Column(name="middle_name", type="string", length=255, nullable=false)
 * @Assert\NotBlank(message="Please enter your middle name", groups={"Registration", "Profile"})
 */
protected $middleName;

/**
 * @ORM\Column(name="last_name", type="string", length=255, nullable=false)
 * @Assert\NotBlank(message="Please enter your last name", groups={"Registration", "Profile"})
 */
protected $lastName;

/**
 * @ORM\Column(name="position", type="string", length=255, nullable=false)
 * @Assert\NotBlank(message="Please enter your position/title", groups={"Registration", "Profile"})
 */
protected $position;

/**
 * @ORM\Column(name="gender", type="string", length=255, nullable=false)
 * @Assert\NotBlank(message="Please chose your gender", groups={"Registration", "Profile"})
 */
protected $gender;

/**
 * @ORM\Column(name="birth_date", type="string", nullable=true)
 */
protected $birthDate;

/**
 * @ORM\Column(name="address", type="string", length=255, nullable=true)
 */
protected $address;

/**
 * @ORM\Column(name="school", type="string", length=255, nullable=true)
 */
protected $school;

/**
 * @ORM\Column(name="hired_date", type="string", nullable=false)
 */
protected $hiredDate;

/**
 * @ORM\Column(name="end_date", type="string", nullable=false)
 */
protected $endDate;

// Change the targetEntity path if you want to create the group

/**
 * @ORM\ManyToMany(targetEntity="UserBundle\Entity\Group")
 * @ORM\JoinTable(name="fos_user_user_group",
 *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id")}
 * )
 */
protected $groups;

public function __construct()
{
    parent::__construct();

}

/**
 * @return String
 */
public function getFirstName()
{
    return $this->firstName;
}

/**
 * @return String
 */
public function getMiddleName()
{
    return $this->middleName;
}

/**
 * @return String
 */
public function getLastName()
{
    return $this->lastName;
}

/**
 * @return String
 */
public function getPosition()
{
    return $this->position;
}

/**
 * @return String
 */
public function getGender()
{
    return $this->gender;
}

/**
 * @return String
 */
public function getBirthDate()
{
    return $this->birthDate;
}

/**
 * @return String
 */
public function getAddress()
{
    return $this->address;
}

/**
 * @return String
 */
public function getSchool()
{
    return $this->school;
}

/**
 * @return String
 */
public function getHiredDate()
{
    return $this->hiredDate;
}

/**
 * @return String
 */
public function getEndDate()
{
    return $this->endDate;
}

/**
 * @param String $firstName
 * @return User
 */
public function setFirstName($firstName)
{
    $this->firstName = $firstName;

    return $this;
}

/**
 * @param String $middleName
 * @return User
 */
public function setMiddleName($middleName)
{
    $this->middleName = $middleName;

    return $this;
}

/**
 * @param String $lastName
 * @return User
 */
public function setLastName($lastName)
{
    $this->lastName = $lastName;

    return $this;
}

/**
 * @param String $position
 * @return User
 */
public function setPosition($position)
{
    $this->position = $position;

    return $this;
}

/**
 * @param String $gender
 * @return User
 */
public function setGender($gender)
{
    $this->gender = $gender;

    return $this;
}

/**
 * @param String $birthDate
 * @return User
 */
public function setBirthDate($birthDate)
{
    $this->birthDate = $birthDate;

    return $this;
}

/**
 * @param String $address
 * @return User
 */
public function setAddress($address)
{
    $this->address = $address;

    return $this;
}

/**
 * @param String $school
 * @return User
 */
public function setSchool($school)
{
    $this->school = $school;

    return $this;
}

/**
 * @param String $hiredDate
 * @return User
 */
public function setHiredDate($hiredDate)
{
    $this->hiredDate = $hiredDate;

    return $this;
}

/**
 * @param String $endDate
 * @return User
 */
public function setEndDate($endDate)
{
    $this->endDate = $endDate;

    return $this;
}
}

这是我的 RegistrationFormType:

<?php

namespace UserBundle\Form;

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

class RegistrationType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('firstName', 'text', array('label' => 'form.first_name', 'translation_domain' => 'FOSUserBundle'))
            ->add('middleName', 'text', array('label' => 'form.middle_name', 'translation_domain' => 'FOSUserBundle'))
            ->add('lastName', 'text', array('label' => 'form.last_name', 'translation_domain' => 'FOSUserBundle'))
            ->add('position', 'text', array('label' => 'form.position', 'translation_domain' => 'FOSUserBundle'))
            ->add('gender', 'text', array('label' => 'form.gender', 'translation_domain' => 'FOSUserBundle'))
            ->add('birthDate', 'date', array('label' => 'form.birth_Date', 'translation_domain' => 'FOSUserBundle'))
            ->add('address', 'text', array('label' => 'form.address', 'translation_domain' => 'FOSUserBundle'))
            ->add('school', 'text', array('label' => 'form.school', 'translation_domain' => 'FOSUserBundle'))
            ->add('hiredDate', 'date', array('label' => 'form.hired_date', 'translation_domain' => 'FOSUserBundle'))
            ->add('endDate', 'text', array('label' => 'form.end_date', 'translation_domain' => 'FOSUserBundle'))
        ;
    }

    public function getParent()
    {
        return 'fos_user_registration';
    }

    public function getName()
    {
        return 'user_registration';
    }
}

这是我的 services.yml:

services:
    app.form.registration:
        class: UserBundle\Form\RegistrationFormType
        tags:
            - { name: form.type, alias: user_registration }

这是我的 config.yml:

fos_user:
db_driver: orm
firewall_name: main
user_class: UserBundle\Entity\User
group:
    group_class: UserBundle\Entity\Group
profile:
    form:
        type: UserBundle\Form\ProfileFormType 
registration:
    form:
        name: user_registration

这是我的 twig:我想调用我创建的所有其他实体

{{ form_widget(form.first_name) }}
{{ form_widget(form.last_name) }}
{{ form_widget(form.middle_name) }}
// and so on...

这样称呼它:{{ form_widget(form.firstName) }}

在您的表单中,它是 firstName,而不是 first_name。姓氏中间名相同...