>我有一个带有多个上传字段的表单,但是当我尝试上传多个文件(或者即使只有一个文件)时,它会显示下一个错误:
数组类型中的异常.php第 45 行:序列化 不允许使用"Symfony\Component\HttpFoundation\File\UploadedFile"
多个上传字段引用属性"图像",它是一个数组。
/**
* @var array
* @ORMColumn(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 SymfonyComponentHttpFoundationFileUploadedFile $avatar */
$avatar = $artist->getAvatar();
/** @var SymfonyComponentHttpFoundationFileUploadedFile $cover */
$cover = $artist->getCoverPhoto();
/** @var SymfonyComponentHttpFoundationFileUploadedFile $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 SymfonyComponentHttpFoundationFileUploadedFile $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()
));
}
和表单类型:
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));
}
对不起,如果我的英语不是很好。我正在学习它。
当您提交表单时,您的$images
属性中有一个UploadedFile
对象的数组。我的建议是映射该集合并执行类似于头像和封面的保存该文件。下面是一个代码示例:
if ($images) {
$imgDir = $this->container->getparameter('kernel.root_dir').'/../web/user/images';
$artist->setImages(array_map(function ($img) use ($imgDir) {
$imgName = md5(uniqid()).'.'.$img->guessExtension();
$img->move($imgDir, $imgName);
return $imgName;
}, $images));
}
$em->persist($artist);
$em->flush();