2017-05-11 86 views
0

我已經做了以下FormType以修改用戶個人資料:FosUserBundle我的自定義用戶實體未實現 ArrayAccess接口

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以下CONFIGS:

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

正如我看到的documentation我可以完全覆蓋由只是不重寫getParent()方法的形式。由於單證說:

If you don't want to reuse the fields added in FOSUserBundle by default, you can omit the getParent method and configure all fields yourself.

,但我得到了以下錯誤:

Cannot read index "name" from object of type "AppUserBundle\Entity\User" because it doesn't implement \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

很老,但我也被困在這方面,我希望能幫助別人。您可以通過添加以下功能

$resolver->setDefaults(array(
     'data_class' => 'YourBundle\Entity\User', 
    )); 
告訴Symfony的您正在使用您的自定義登記表與您的用戶類