dustin10 / VichUploaderBundle

A simple Symfony bundle to ease file uploads with ORM entities and ODM documents.
MIT License
1.83k stars 518 forks source link

Problem during reading with flysystem #1424

Open Servialux opened 6 months ago

Servialux commented 6 months ago
Q A
Bundle version 2.3.0
Symfony version v7.0.2
League/flysystem-bundle 3.3.2
PHP version 8.3.1

I've looked everywhere but unfortunately I can't find my answer. I tried to debug it myself but without success.

I can write correctly in the db and in my ObjectStorage

I've done the classical way (not my first time) to get FlySystem and VichUploader working. I've come to notice that when I request reading entity, VichUploader is not called at all. As a result, I get the following error:

{"message": "Cannot assign string to property App\Entity\Shops::$imageFile of type ?Symfony\Component\HttpFoundation\\File"}

Thank you in advance for an indication or look I attach my files below.

Best regards, Nestate

Support Question

//Entity/shops
<?php

namespace App\Entity;

use Symfony\Bridge\Doctrine\Types\UuidType;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Uid\Uuid;
use Symfony\Component\Validator\Constraints as Assert;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
use Doctrine\ORM\Mapping as ORM;

use App\Repository\ShopsRepository;
use Symfony\Component\HttpFoundation\File\File;

#[ORM\Entity(repositoryClass: ShopsRepository::class)]
#[Vich\Uploadable]
class Shops implements EntityFilesInterface
{
// Some var
    #[ORM\Column(length: 255, nullable: true)]
    #[Groups(['get:shop', 'is:file'])]
    #[Vich\UploadableField(mapping: 'shopImage', fileNameProperty: 'imageName', size: 'imageSize')]
    private ?File $imageFile = null;
//Other var and function 
   // Image Functions

    /**
     * If manually uploading a file (i.e. not using Symfony Form) ensure an instance
     * of 'UploadedFile' is injected into this setter to trigger the update. If this
     * bundle's configuration parameter 'inject_on_load' is set to 'true' this setter
     * must be able to accept an instance of 'File' as the bundle will inject one here
     * during Doctrine hydration.
     *
     * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile|null $imageFile
     */
    public function setImageFile(?File $imageFile = null): void
    {
        if ($imageFile instanceof File) {
            // 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
            $this->updatedAt = new \DateTimeImmutable();
        }
        $this->imageFile = $imageFile;
    }

    public function getImageFile(): ?File
    {
        return $this->imageFile;
    }

    public function setImageName(?string $imageName): void
    {
        $this->imageName = $imageName;
    }

    public function getImageName(): ?string
    {
        return $this->imageName;
    }

    public function setImageSize(?int $imageSize): void
    {
        $this->imageSize = $imageSize;
    }

    public function getImageSize(): ?int
    {
        return $this->imageSize;
    }

My vich_uploader configuration file:


vich_uploader:
    db_driver: orm
    storage: flysystem

    metadata:
        type: attribute

    mappings:
        shopImage:
            uri_prefix: /img/shops/image
            upload_destination: shops.storage
            namer: Vich\UploaderBundle\Naming\SmartUniqueNamer

            inject_on_load: true
            delete_on_update: true
            delete_on_remove: true
        logoImage:
            uri_prefix: /img/shops/logo
            upload_destination: shops.storage
            namer: Vich\UploaderBundle\Naming\SmartUniqueNamer

            inject_on_load: true
            delete_on_update: true
            delete_on_remove: true

And in a controller as i do:

 $this->em->getRepository(Shops::class)->findAll()

I got the error I've written on top of my message like the bundle is not used for getting from DB I've used them in two projects before this one and I don't understand why this time it doesn't work

Servialux commented 6 months ago

Hello everyone,

Well, I'm still trying to figure out where the problem is coming from and I'm trying to fix it. It seems that the problem comes from BaseListener to the function getUploadableFields it is not triggered. On my other projects it is. I don't know if I'm on the right track.

Servialux commented 6 months ago

Hello everyone.

I have found a temporary solution. But I don't like it too much.

I just added:

/**
    * @var string|File|null
*/ 
#[ORM\Column(length: 255, nullable: true)]
    #[Groups(['get:shop', 'is:file'])]
    #[Vich\UploadableField(mapping: 'shopImage', fileNameProperty: 'imageName', size: 'imageSize')]
    private $imageFile = null;

It's not a problem, but I'd really like to understand why Vich no longer does this automatically. It doesn't trigger getUploadbleFields.

I'll wait for an answer if someone encounters the same problem.

Have a nice day

garak commented 2 weeks ago

@Servialux does the solution mentioned here work for you?

Servialux commented 2 weeks ago

Hi,

I haven't seen the answer. For the moment it work with the annotation I can test it next week. And I tell here if it works

Thanks for your answer