2012-06-27 2 views
8

하나의 양식 유형을 표시하려고하지만 사용자가 한 번에 패치 업로드를 업로드해야하는 경우가 있습니다. 30 개의 파일을 업로드하고 30 개의 페이지를 작성하십시오.양식 모음 오류

The form's view data is expected to be of type scalar, array or an instance of \ArrayAccess, but is an instance of class MS\CoreBundle\Entity\Photo. You can avoid this error by setting the "data_class" option to "MS\CoreBundle\Entity\Photo" or by adding a view transformer that transforms an instance of class MS\CoreBundle\Entity\Photo to scalar, array or an instance of \ArrayAccess.

갤러리 형식 코드는 다음과 같습니다 :

public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder->add('photo', 'collection', array(
     'type' => new PhotoType(), 
     'allow_add' => true, 
     'data_class' => 'MS\CoreBundle\Entity\Photo', 
     'prototype' => true, 
     'by_reference' => false, 
    )); 
} 

사진 유형 코드는 다음과 같습니다

public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder->add('description', 'text', array('label' => "Title:", 'required' => true)) 
       ->add('File') 
       ->add('album', 'entity', array(
        'class' => 'MSCoreBundle:Album', 
        'property' => 'title', 
        'required' => true, 
        'query_builder' => function(EntityRepository $er) 
        { 
         return $er->createQueryBuilder('a') 
          ->orderBy('a.title', 'ASC'); 
        }, 
       )) 
     ; 
    } 

    public function setDefaultOptions(OptionsResolverInterface $resolver) 
    { 
     $resolver->setDefaults(array(
      'data_class' => 'MS\CoreBundle\Entity\Photo', 
     )); 
    } 

내 컨트롤러 기능은 다음과 같습니다

 public function newAction($count) 
     { 
      for($i = 1; $i <= $count; $i++) { 
       $entity = new Photo(); 
      } 

      $form = $this->container->get('ms_core.gallery.form'); 
      $form->setData($entity); 

      return array(
       'entity' => $entity, 
       'form' => $form->createView() 
      ); 


    } 
이 오류를 수신하고

도움이 될 것입니다.

답변

11

갤러리 유형의 collection typedata_class 옵션을 전달하면 안됩니다. 당신이 (당신이하지 말았어야하므로, 이미 설정 됨) 포토 타이프의 기본값을 대체 할 경우 또는, 당신과 같이 옵션 배열을 지정할 수 있습니다

public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder->add('photo', 'collection', array(
     'type' => new PhotoType(), 
     'allow_add' => true, 
     'options' => array('data_class' => 'MS\CoreBundle\Entity\Photo'), 
     'prototype' => true, 
     'by_reference' => false, 
    )); 
} 

당신이을 확인 "GalleryType"에 기본값 data_class 옵션이 설정되어 있으면 앨범이어야합니다.

또한 컨트롤러에서 양식을 올바르게 작성하지 않습니다. 양식의 데이터 유형 (이 경우 앨범)을 사용하여 setData()으로 전화해야합니다.

public function newAction($count) 
{ 
     $album = new Album(); 
     for($i = 1; $i <= $count; $i++) { 
      $album->addPhoto(new Photo()); 
     } 

     $form = $this->container->get('ms_core.gallery.form'); 
     $form->setData($album); 

     return array(
      'entity' => $album, 
      'form' => $form->createView() 
     ); 
}