Search code examples
phpsymfonysymfony4vichuploaderbundle

Multiple upload the files are not saved


I try to save multiple files using VichUploader Bundle. The name of the file is well saved in database but unfortunately, the file is not moved into the correct folder.

I use Symfony 4 & VichUploaderBundle 1.8.3.

Here is my Album class :

/**
 * @ORM\Entity(repositoryClass="App\Repository\AlbumRepository")
 * @ORM\HasLifecycleCallbacks
 */
class Album
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
     private $id;

    /**
     * @ORM\OneToMany(targetEntity="App\Entity\Photo", mappedBy="album", orphanRemoval=true, cascade={"persist", "remove"})
     */
    private $photos;

    /**
     * @ORM\Column(type="datetime")
     */
    private $createdAt;

    /**
     * @ORM\Column(type="text", nullable=true)
     */
    private $description;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $title;

...
}

And now, the Photo

/**
 * @ORM\Entity(repositoryClass="App\Repository\PhotoRepository")
 * @Vich\Uploadable()
 * @ORM\HasLifecycleCallbacks
 */
class Photo
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $imageName;

    /**
     * @Vich\UploadableField(mapping="album_images", fileNameProperty="imageName")
     * @var File
     */
    private $imageFile;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Album", inversedBy="photos")
     * @ORM\JoinColumn(nullable=false)
     */
    private $album;

    /**
     * @ORM\Column(type="datetime")
     * @var \DateTime
     */
    private $updatedAt;
...
}

Ok, so, in order to use the Bundle, I wrote a DataTransformer that transform the UploadedFile into a Photo. This way, I can manipulate an array of Photo.

This PhotoTransformer is quite simple, I just want to move the files from Front to Back-end

class PhotoTransformer implements DataTransformerInterface
{
    public function transform($value)
    {
        //We won't reverse the operation. It goes from front-end to back-end.
        return [];
    }

    public function reverseTransform($value)
    {
        $photos = [];
        foreach ($value as $file) {
            $photo = new Photo();
            $photo->setImageFile($file);
            $photos[] = $photo;
        }
        return $photos;
    }
}

And here is my AlbumType :

class AlbumType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        return $builder
            ->add('title', TextType::class)
            ->add('description', TextareaType::class)
            ->add($builder->create(
                    'photos', FileType::class, [
                        'required' => false,
                        'multiple' => true
                    ]
                )->addModelTransformer(new PhotoTransformer())
            )
            ->add('button', SubmitType::class, [
                'label' => 'Create'
            ]);
    }
}

As you can see, I followed the rule wrote here. The var passed to the "setImageFile" is an UploadedFile.

Yet, unfortulately, this doesn't trigger the upload on the server.

Do you know what I'm missing?


Solution

  • The problem was in the vich_uploader.yaml, the upload_destination was not the correct one. So I checked at one place and the files were uploaded at another.

    Everything works fine now.