1

i simply try to update/delete a file in sonata admin with VichUploader bundle.

But in both cases i have this error : Expected argument of type "string", "null" given at property path "filename".

note :

  • on delete, the entity is delete anyway and the file is correctly moved to trash after this error, but update is impossible, there is just this error
  • on update, the media->setFile() is called and the media->updatedAt is modified before triggering the error

Here is my entity :


<?php

namespace My\CoreBundle\Entity;

use Doctrine\DBAL\Types\Types;
use My\CoreBundle\Repository\MediaRepository;
use Doctrine\ORM\Mapping as ORM;
use Knp\DoctrineBehaviors\Contract\Entity\TimestampableInterface;
use Knp\DoctrineBehaviors\Model\Timestampable\TimestampableTrait;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
use Symfony\Component\HttpFoundation\File\File;

#[ORM\Table(name: 'my_media')]
#[Vich\Uploadable]
#[ORM\Entity(repositoryClass: MediaRepository::class)]
class Media implements TimestampableInterface
{
    use TimestampableTrait;

    #[Vich\UploadableField(mapping: 'media', fileNameProperty: 'filename')]
    private ?File $file = null;

    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column(length: 255,  unique: true)]
    private ?string $name = null;

    #[ORM\Column(type: Types::TEXT)]
    private ?string $caption = null;

    #[ORM\Column(length: 255, unique: true)]
    private ?string $filename = null;

    // ##################### Custom methods #####################
    public function __toString()
    {
        return $this->name;
    }

    public function getUrl(): ?string
    {
        return sprintf('/uploads/images/%s', $this->filename);
    }

    public function setFile(?File $file = null): void
    {
        $this->file = $file;
        
        if (null !== $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();
        }
    }

    public function getFile(): ?File
    {
        return $this->file;
    }

    // ##################### generated methods #####################
    public function getId(): ?int
    {
        return $this->id;
    }

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(string $name): self
    {
        $this->name = $name;

        return $this;
    }

    public function getCaption(): ?string
    {
        return $this->caption;
    }

    public function setCaption(string $caption): self
    {
        $this->caption = $caption;

        return $this;
    }

    public function getFilename(): ?string
    {
        return $this->filename;
    }

    public function setFilename(string $filename): self
    {
        $this->filename = $filename;

        return $this;
    }
}

The admin controller :

// [...]
final class MediaAdmin extends AbstractAdmin
{

    protected function configureFormFields(FormMapper $formMapper): void
    {
        $formMapper
            ->add('name')
            ->add('caption')
            ->add('file', VichImageType::class, ['required' => false])
            ->end();
    }
// [...]
}

Vich config :

vich_uploader:
    db_driver: orm
    storage: flysystem

    mappings:
       media:
           uri_prefix: /uploads/images
           upload_destination: default.storage
           namer: Vich\UploaderBundle\Naming\SmartUniqueNamer

SOLUTION

the entity's "filename" property NEEDS to be nullable if using vichuploader. WARNING : don't forget to edit the setter allowing nullable parameter with ?

correct entity :


<?php

namespace My\CoreBundle\Entity;

// […]

#[ORM\Table(name: 'my_media')]
#[Vich\Uploadable]
#[ORM\Entity(repositoryClass: MediaRepository::class)]
class Media implements TimestampableInterface
{
    use TimestampableTrait;

    #[Vich\UploadableField(mapping: 'media', fileNameProperty: 'filename')]
    private ?File $file = null;

    //[…]

    #[ORM\Column(length: 255, nullable: true)]
    private ?string $filename = null;

    //[…]

    public function setFilename(?string $filename): self // <<<<<< IMPORTANT : DONT FORGET '?' or doctrine will trigger not nullable error !
    {
        $this->filename = $filename;

        return $this;
    }
}
mysterty
  • 31
  • 7

0 Answers0