Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem during reading with flysystem #1424

Open
Servialux opened this issue Jan 12, 2024 · 2 comments
Open

Problem during reading with flysystem #1424

Servialux opened this issue Jan 12, 2024 · 2 comments
Labels

Comments

@Servialux
Copy link

Servialux commented Jan 12, 2024

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

@garak garak added the Support label Jan 12, 2024
@Servialux
Copy link
Author

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
Copy link
Author

Servialux commented Jan 15, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants