【问题标题】:FosUserBundle My custom User Entity does not Implement \ArrayAccessFosUserBundle 我的自定义用户实体没有实现 \ArrayAccess
【发布时间】:2017-05-11 17:24:00
【问题描述】:

为了编辑用户个人资料,我制作了以下 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;
    }

【问题讨论】:

    标签: php symfony fosuserbundle


    【解决方案1】:

    非常老,但由于我也被困在这个问题上,我希望能帮助别人。您必须通过添加以下函数来告诉 Symfony 您正在使用自定义注册表单和 User 类

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-06
      相关资源
      最近更新 更多