Symfony - 多个文件上传时不允许序列化

时间:2017-01-12 10:51:28

标签: php file symfony doctrine-orm

我有一个包含多个上传字段的表单,但是当我尝试上传多个文件时(或者即使它只是一个),它会显示下一个错误:

  

ArrayType.php第45行中的异常:序列化   不允许使用'Symfony \ Component \ HttpFoundation \ File \ UploadedFile'

多个上传字段会返回属性'images',它是一个数组。

/**
 * @var array
 * @ORM\Column(name="images", type="array", nullable=true)
 */
protected $images;

/**
 * Set images
 *
 * @param array $images
 * @return Artist
 */
public function setImages($images)
{
    $this->images = $images;

    return $this;
}

/**
 * Get images
 *
 * @return array 
 */
public function getImages()
{
    return $this->images;
}

在树枝上我用这个:

{{ form_label(form.images) }}
<br>
{{ form_errors(form.images) }}
{{ form_widget(form.images) }}
<br>

我用于表单的操作:

public function editAction(Request $request)
    {
        $em = $this->getDoctrine()->getManager(); 

        $user = $this->getUser();
        $artist = $em->getRepository('CASUsuariosBundle:Artist')->find($user);

        if (!is_object($user) || !is_object($artist) || !$user instanceof UserInterface) {
            throw new AccessDeniedException('This user does not have access to this section.');
        }

        $avatar1 = $artist->getAvatar();
        $cover1 = $artist->getCoverPhoto();
        $images1 = $artist->getImages();

        $form = $this->createForm(ArtistProfileType::class, $artist);
        $form->setData($artist);

        $form->handleRequest($request);

        if ($form->isValid()) {

            /** @var Symfony\Component\HttpFoundation\File\UploadedFile $avatar */
            $avatar = $artist->getAvatar();
            /** @var Symfony\Component\HttpFoundation\File\UploadedFile $cover */
            $cover = $artist->getCoverPhoto();
            /** @var Symfony\Component\HttpFoundation\File\UploadedFile $images */
            $images = $artist->getImages();


            if( $avatar === null )
            {

                $artist->setAvatar($avatar1);
            }
            else
            {

                $avatarName = md5(uniqid()).'.'.$avatar->guessExtension();

                $avDir = $this->container->getparameter('kernel.root_dir').'/../web/user/avatars';
                $avatar->move($avDir, $avatarName);
                $artist->setAvatar($avatarName);

            }

            if( $cover === null ) {

                $artist->setCoverPhoto($cover1);

            }
            else{

                /** @var Symfony\Component\HttpFoundation\File\UploadedFile $cover */
                $cover = $artist->getCoverPhoto();

                $coverName = md5(uniqid()).'.'.$cover->guessExtension();

                $coDir = $this->container->getparameter('kernel.root_dir').'/../web/user/covers';                
                $cover->move($coDir, $coverName);                
                $artist->setCoverPhoto($coverName);

            }

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

            $url = $this->generateUrl('show_artist');
            $response = new RedirectResponse($url);
            return $response;
        }

        return $this->render('CASUsuariosBundle:Profile:edit_profile_artist.html.twig', array(
            'form' => $form->createView()
        ));
    }

formType:

public function buildForm(FormBuilderInterface $builder, array $options)
    {

        $builder->add('categories', EntityType::class, array(
            'label' => 'Categoria',
            'class' => 'CASEventBundle:Category',
            'choice_label' => 'name',
            'multiple' => true
            ));
        $builder->add('subcategories', EntityType::class, array(
            'label' => 'Subcategorias',
            'class' => 'CASEventBundle:Subcategory',
            'choice_label' => 'name',
            'multiple' => true,
            'expanded' => true
            ));
        $builder->add('short_description', null, array('label' => 'Descripción corta'));
        $builder->add('description', null, array('label' => 'Descripción'));
        $builder->add('provinces', EntityType::class, array(
            'label' => 'Provincia',
            'class' => 'CASEventBundle:Province',
            'choice_label' => 'name',
            'required' => false,
            'multiple' => true,
            'expanded' => true
            ));
        $builder->add('city', null, array('label' => 'Ciudad'));
        $builder->add('avatar', FileType::class, array(
            "label" => "Imagen de perfil:",
            'required' => false,
            "data_class" => null
        ));
        $builder->add('cover_photo', FileType::class, array('required' => false, 
            'label' => 'Imagen de portada',
            'required' => false,
            'data_class' => null));
        $builder->add('images', FileType::class, array(
            'multiple' => true,
            'required' => false,
            "data_class" => null,
            'label' => 'Imagenes'
        ));
        $builder->add('members', null, array('required' => false, 'label' => 'Nº de miembros'));
        $builder->add('oficial_web', null, array('required' => false, 'label' => 'Página web'));
        $builder->add('facebook', null, array('required' => false));
        $builder->add('youtube', CollectionType::class, array(
            'required' => false,
            'allow_add' => true,
            'allow_delete' => true));
        $builder->add('soundcloud', CollectionType::class, array(
            'prototype' => true,
            'required' => false,
            'allow_add' => true,
            'allow_delete' => true));

    }

对不起,如果我的英语不是很好。我正在学习它。

1 个答案:

答案 0 :(得分:1)

当您提交表单时,您$属性中有一组UploadedFile个对象。我建议的是映射该集合并执行保存类似于头像和封面的文件。这是一个代码示例:

$images