I'm using EasyAdminBundle in Symfony 4 and I would like to create a multiple image upload in my form with VichUploaderBundle (or without), but I can't find any updated documentation for Symfony 4, I do not know what to do to make it work.
I created a Photo entity and a Product entity with the relationship many-to-many unidirectional :
Product.php :
<?php
declare(strict_types = 1);
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Class Product
*
* @package App\Entity
*
* @ORM\Entity
*/
class Product
{
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer", options={"unsigned":true}, nullable=false)
*
* @var int
*/
protected $id;
/**
* @ORM\ManyToMany(targetEntity="Photo", cascade={"persist"})
*
* @var Photo[]|ArrayCollection
*/
protected $photos;
/**
* Product constructor.
*/
public function __construct()
{
$this->photos = new ArrayCollection();
}
Photo.php :
<?php
declare(strict_types = 1);
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* Photo
*
* @ORM\Entity()
* @Vich\Uploadable
*/
class Photo
{
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*
* @var int
*/
protected $id;
/**
* @ORM\Column(name="name", type="string", length=255)
*
* @var string
*/
protected $name;
/**
* @ORM\Column(type="string", length=255)
*
* @var string
*/
protected $photo;
/**
* @Vich\UploadableField(mapping="photos", fileNameProperty="photo")
*
* @var File
*/
protected $photoFile;
/**
* @ORM\Column(type="datetime", length=255)
*
* @var \DateTime
*/
protected $updatedAt;
/**
* @return int
*/
public function getId(): ?int
{
return $this->id;
}
/**
* @param int $id
*/
public function setId(int $id): void
{
$this->id = $id;
}
/**
* @return string
*/
public function getName(): ?string
{
return $this->name;
}
/**
* @param string $name
*/
public function setName(string $name): void
{
$this->name = $name;
}
/**
* @param File|null $photo
* @return Photo
*/
public function setPhotoFile(File $photo = null)
{
$this->photoFile = $photo;
if ($photo) {
$this->updatedAt = new \DateTime('now');
}
return $this;
}
/**
* @return File
*/
public function getPhotoFile() : ?File
{
return $this->photoFile;
}
/**
* @param string $photo
* @return Photo
*/
public function setPhoto($photo)
{
$this->photo = $photo;
return $this;
}
/**
* @return string
*/
public function getPhoto() : ?string
{
return $this->photo;
}
}
The vich_uploader.yaml file :
vich_uploader:
db_driver: orm
mappings:
photos:
uri_prefix: "/products/"
upload_destination: '%kernel.root_dir%/../public/build/images/products/'
namer: vich_uploader.namer_uniqid
easy_admin.yaml :
easy_admin:
entities:
Product:
class: App\Entity\Product
form:
fields:
- { property: 'photos', type: 'collection', type_options: { entry_type: 'App\Form\PhotoType' } }
PhotoType.php :
<?php
declare(strict_types = 1);
namespace App\Form;
use App\Entity\Product;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Vich\UploaderBundle\Form\Type\VichFileType;
/**
* Class PhotoType
*
* @package App\Form
*/
class PhotoType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('photos', CollectionType::class, ['entry_type' => VichFileType::class])
;
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Photo::class,
]);
}
}