FosUserBundle我的自定义用户实体不实现\ ArrayAccess

时间:2017-05-11 17:24:00

标签: php symfony fosuserbundle

我制作了以下FormType以编辑用户个人资料:

namespace AppUserBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;

class UserProfileFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        //$builder->addViewTransformer($this->purifierTransformer);
        $builder->add('name');
        $builder->add('surname');
        $builder->add('description',TextareaType::class);
    }

//  public function getParent()
//  {
//      return 'FOS\UserBundle\Form\Type\ProfileFormType';
//  }

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

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

我已将其设置为这样的服务:

  app_user.user_profile_form:
    class: AppUserBundle\Form\Type
    tags:
      - { name: form.type, alias: app_user_registration }

config.yml中的以下配置:

fos_user:
  ...
  profile:
    form:
      type: AppUserBundle\Form\Type\UserProfileFormType

正如我在documentation上看到的那样,我可以通过不覆盖getParent()方法完全覆盖表单。正如文件所说:

  

如果您不想重复使用FOSUserBundle中添加的字段,则可以省略getParent方法并自行配置所有字段。

但是我收到以下错误:

  

无法读取索引"名称"来自类型" AppUserBundle \ Entity \ User"因为它没有实现\ ArrayAccess。

请注意,AppUserBundle\Entity\User包含以下代码:

namespace AppUserBundle\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(name="user_image", type="string", length=255, nullable=true)
     */
    private $userImage;

    /**
     * @ORM\Column(name="name", type="string", length=255, nullable=true)
     * @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"}
     * )
     */
    private $name;

    /**
     * @ORM\Column(name="surname", type="string", length=255, nullable=true)
     * @Assert\NotBlank(message="Please enter your name.", groups={"Registration", "Profile"})
     * @Assert\Length(
     *     min=3,
     *     max=255,
     *     minMessage="The surname is too short.",
     *     maxMessage="The surname is too long.",
     *     groups={"Registration", "Profile"}
     * )
     */
    private $surname;

    /**
     * @ORM\Column(name="description", type="string", length=1024, nullable=true)
     */
    private $description;

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

    public function setUserImage($imagepath)
    {
        $this->userImage=$imagepath;
    }

    public function functiongetUserImage()
    {
        return $this->userImage;
    }

    public function setName($name)
    {
        $this->name=strip_tags($name);
    }

    public function getName()
    {
        return $this->name;
    }

    public function setSurname($surname)
    {
        $this->surname=strip_tags($surname);
    }

    public function getSurname()
    {
        return $this->surname;
    }

    public function setDescription($description)
    {
        $this->description=$description;
    }

    public function getDescription()
    {
        return $this->description;
    }

1 个答案:

答案 0 :(得分:1)

很老但是因为我也坚持这个我希望能帮助别人。您必须通过添加以下功能告诉Symfony您正在将自定义注册表用于您的用户类

 $resolver->setDefaults(array(
        'data_class' => 'YourBundle\Entity\User',
    ));