0
votes

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

1
show us your form typekamwoz
I edited my postElGecko_76
You received files from form, but relation is Image. Use VichuploadBundle to upload files.zalex
I have to depend on a bundle? I can not create that myself?ElGecko_76
I managed to insert in the table "Album" and "Image" but not in the linking table 'album_image ", why?ElGecko_76

1 Answers

1
votes

Your images attribute should be something like this

->add('images', CollecionType::class, [
'entry_type' => ImageType::class,
'allow_add' => true,
'allow_delete' => true

])

And then your ImageType::class should be like this

->add('image', FileType::class, [
'attr' => [
    'placeholder' => 'Images de l\'album *',
    'accept' => 'image/*',
    'multiple' => 'multiple
], 'required' => false, 'data_class' => null, 'multiple' => true,])