2017-08-18 7 views
0

プロフィールエンティティの編集フォームを読み込もうとしているときに、このエラーが表示されています。プロフィールフォームを読み込む際のsymfonyエラー

The form's view data is expected to be an instance of class AppBundle\Entity\Profile, but is a(n) array. You can avoid this error by setting the "data_class" option to null or by adding a view transformer that transforms a(n) array to an instance of AppBundle\Entity\Profile. 

これをどのように適合させるかわかっている人は誰でも気になりました。私はプロファイルコントローラを使用しており、ユーザとプロファイルは互いにOneToOne関係にあります。ここで

はフォーム

/** 
* @Route("/profile/edit", name="profile_edit") 
*/ 
public function editAction(Request $request) 
{ 

    $em = $this->getDoctrine()->getManager(); 
    $profileRepository = $em->getRepository(Profile::class); 

    $user = $this->getUser(); 

    $profile = $profileRepository->getProfileByUserId($user->getId()); 

    $form = $this->createForm(ProfileType::class, $profile); 

    $form->handlerequest($request); 

    if($form_.isSubmitted() && $form->isValid()) { 

     $firstname = $form->get('firstname')->getData(); 
     $lastname = $form->get('lastname')->getData(); 

     $description = $form->get('description')->getData(); 

     $profile->setFirstname($firstname); 
     $profile->setLastName($lastname); 
     $profile->setDescription($description); 

     $em->persist($profile); 
     $em->flush(); 

     $this->addFlash('flash-profileeditted', 'You\'ve successfully updated your profile.'); 
     $this->redirectToRoute('profile_page'); 
    } 

    return $this->render('profile/edit.html.twig', ['form' => createForm(), 'profile' => $profile]); 
} 

そして、ここでは私のprofileTypeがあることをロードするプロファイルコントローラ::クラス

public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder 
    ->add('firstname', TextType::class, [ 'label' => 'Firstname', 'attr' => ['class' => 'form-control']]) 
    ->add('lastname', TextType::class, ['label' => 'Lastname', 'attr' => ['class' => 'form-control']]) 
    ->add('description', TextareaType::class, ['label' => 'In Your Own Words', 'attr' => ['class' => 'form-control']]) 
    ->add('user') 
    ->add('submit', SubmitType::class, ['label' => 'Edit Profile', 'attr' => ['class' => 'btn btn-info']]); 
} 

/** 
* {@inheritdoc} 
*/ 
public function configureOptions(OptionsResolver $resolver) 
{ 
    $resolver->setDefaults(array(
     'data_class' => 'AppBundle\Entity\Profile' 
    )); 
} 

/** 
* {@inheritdoc} 
*/ 
public function getBlockPrefix() 
{ 
    return 'appbundle_profile'; 
} 

ないうまくいけば、すべてがOKです他に何、ここで含まれるように、確かに私のコードです解決策はこのコード内で見つけることができます。

私はFOSUserBundleを使用していることを指摘しておきます。事前に

おかげで、[OK]を

答えて

0

。私は最終的に何が起こっているかを知った。私はプロファイルリポジトリを使用してIDでユーザーを検索していましたが、配列を返していました。それでは、私がしなければならなかったことは、このコードを使用している:

$profile = $profileRepository->findOneByUser($user->getId()); 

これは実際に、フォームを移入するために使用することができAppBundle \エンティティ\プロファイルオブジェクトとしてオブジェクトを返します。

関連する問題