0

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

4
  • You received files from form, but relation is Image. Use VichuploadBundle to upload files. Commented Nov 19, 2017 at 12:48
  • I have to depend on a bundle? I can not create that myself? Commented Nov 19, 2017 at 13:11
  • I managed to insert in the table "Album" and "Image" but not in the linking table 'album_image ", why? Commented Nov 19, 2017 at 20:57
  • you sure you added images to album? $album->addImage($image) and from other side $image->setAlbumt($album) Commented Nov 21, 2017 at 7:08

1 Answer 1

1

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,])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.