I would like create a form with multiple file upload in Symfony 3, but it's not working, I have an error in my controller :
Expected value of type "Doctrine\Common\Collections\Collection|array" for association field "SGBundle\Entity\Album#$images", got "Symfony\Component\HttpFoundation\File\UploadedFile" instead.
I have 2 entities : Album.php and Image.php with the OneToMany relation (an album has several images).
My images are successfully uploaded to my folder but I can not insert it into my database.
Here is the most important part of my code in my controller :
$images = $album->getImages();
foreach ($images as $image) {
$fileName = md5(uniqid('img_album_', true));
$fileExtension = $image->guessExtension();
$nomImageComplet = $fileName . '.' . $fileExtension;
$image->move(
$this->getParameter('albums_images_directory'),
$nomImageComplet
);
$tinified = fromFile($this->getParameter('albums_images_directory') . '/' . $nomImageComplet);
$tinified->toFile($this->getParameter('albums_images_directory') . '/' . $nomImageComplet);
$img = new Image();
$img->setExtension($fileExtension);
$img->setFile($fileName);
$em->persist($img);
$em->flush();
}
My FormType :
/**
* Class AlbumType
*
* @package SGBundle\Form
*/
class AlbumType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->
add('titre', TextType::class, [
'attr' => [
'placeholder' => 'Titre de l\'album (140 caractères max) *',
],
])
->add('miniature', FileType::class, [
'attr' => [
'placeholder' => 'Miniature de l\'album *',
], 'required' => false, 'data_class' => null,
])
->add('images', FileType::class, [
'attr' => [
'placeholder' => 'Images de l\'album *',
'accept' => 'image/*',
'multiple' => 'multiple'
], 'required' => false, 'data_class' => null, 'multiple' => true,
])
->add('envoyer', SubmitType::class)
->add('annuler', ResetType::class);
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(
[
'data_class' => Album::class,
]
);
}
Thank you in advance for your help