Problem with VichUploader (symfony 4)
I have problem with combining VichUploader with EasyAdmin in Symfony 4 (in symfony 3 also, but there I managed to make it work).
I opened an issue in VichUploader repository but I was suggested to also ask here for help, so please, check this one: https://github.com/dustin10/VichUploaderBundle/issues/931#issue-387067754
As said in documentation (https://symfony.com/doc/master/bundles/EasyAdminBundle/integration/vichuploaderbundle.html#uploading-the-images-in-the-edit-and-new-views)
You should use vich_image type in "form" views (form/edit/new)
# config/packages/easy_admin.yaml
easy_admin:
entities:
Product:
# ...
form:
fields:
- { property: 'imageFile', type: 'vich_image' }
# ...
and "image" type in show views (list/show)
# config/packages/easy_admin.yaml
easy_admin:
entities:
Product:
# ...
list:
fields:
- { property: 'image', type: 'image', base_path: '%app.path.product_images%' }
# ...
show:
fields:
- { property: 'image', type: 'image', base_path: '%app.path.product_images%' }
You should explicit set vich_image type on form views as @juanmiguelbesada suggest.
The updateAt issue is solved by doing this on your entity:
....
public function setImageFile(File $image = null)
{
$this->imageFile = $image;
// VERY IMPORTANT:
// It is required that at least one field changes if you are using Doctrine,
// otherwise the event listeners won't be called and the file is lost
if ($image) {
// if 'updatedAt' is not defined in your entity, use another property
$this->setUpdatedAt(new \DateTime('now'));
}
}
...
Another way is overriding updateEntity on AdminController for your entity as in Creating Complex and Dynamic Backends. When I need to do something like this I use specific controller for that entity:
easy_admin:
entities:
UserProfile:
...
class: App\Entity\User
controller: App\Controller\UserProfileController
...
As you're saving Image size I encourage this option because you can detect that size before persist and skip the field on your form view. You must define on an explicit way which fields requires user input and fill the others using overriding technique. If you like a practical example check my EasyAdmin Skeleton App. Check code related to UserProfile and AdminUser configuration. Both refers to same entity and relays on different controllers.
@Aerogirl10 Did you managed to solve this problem :)?
@ktrzos I tried what juanmiguelbesada suggested but it still didn’t worked the way it should. I must say that last time I worked on this project over 2 months ago as I kind of gave up after not making it work... so I thought it must be something silly with my code like typo or so but I am not sure for now. Eventually I gave up using VichUploader as making it ‘by myself’ appeard to not be that hard :)
Do you have the same problem?
To be sincere, I had no issues with that library. Only asked to know whether this case can be closed as "fixed" :).
Nevertheless, I've just implemented use case with VichUploader. It is limited to these steps:
1. Added Vich to bundle
composer require vich/uploader-bundle
2. Filled configuration inside config/packages/vich_uploader.yaml
parameters:
app.path.user_logo: /upload/user/logo
vich_uploader:
db_driver: orm
mappings:
user_logo:
uri_prefix: "%app.path.user_logo%"
upload_destination: "%kernel.root_dir%/../public%app.path.user_logo%"
namer: vich_uploader.namer_uniqid
3. Filled EasyAdmin config
easy_admin:
entities:
User:
class: App\Entity\User
form:
fields:
- property: username
type: email
- property: imageFile
type: Vich\UploaderBundle\Form\Type\VichImageType
4. Filled Doctrine config
App\Entity\User:
fields:
image:
type: string
length: 120
nullable: true
5. Added properties to User class
<?php
namespace App\Entity;
/* other uses here */
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Security\Core\User\UserInterface;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @Vich\Uploadable
*/
class User implements UserInterface, \Serializable
{
/** @var \DateTimeInterface */
protected $updateDate;
/** @var string */
private $image;
/**
* @Vich\UploadableField(mapping="user_logo", fileNameProperty="image")
* @Assert\Valid
* @Assert\File(
* maxSize="1000K",
* mimeTypes={
* "image/jpg", "image/gif", "image/jpeg", "image/png"
* }
* )
* @var File
*/
private $imageFile;
public function setUpdateDate(\DateTimeInterface $updateDate): void
{
$this->updateDate = $updateDate;
}
public function setImageFile(File $image = null): void
{
$this->imageFile = $image;
if($image) {
$this->setUpdateDate(new \DateTimeImmutable());
}
}
public function getImageFile(): ?File
{
return $this->imageFile;
}
public function setImage(?string $image): void
{
$this->image = $image;
}
public function getImage(): ?string
{
return $this->image;
}
public function serialize()
{
return serialize([
$this->id,
$this->username,
$this->password,
$this->active,
]);
}
public function unserialize($serialized): void
{
[
$this->id,
$this->username,
$this->password,
$this->active,
] = \unserialize($serialized, [self::class]);
}
}
(in here, the \Serializable interface is important because of issue appearing when serializing user - by default File will be also serialized, and it should not)
6. Created public/uploads/user/logo directory and set proper write permissions.
After all of these steps, everything is working fine on my test project. I.e. I have uploaded single image to one of my users.
Either way, please, close this issue, or check whether you can manage with your Vich problem again ;).