dustin10 / VichUploaderBundle

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

Vich uploader with easy admin issue #939

Closed serhiobb closed 5 years ago

serhiobb commented 5 years ago

I am using vich-uploader bundle with easy-admin with symfony4. After setup i'm getting this error when trying to delete or update entity

Expected argument of type "string", "NULL" given at property path "imageName".

The config of application is most easy for this common case. Mine Test entity:

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;

/**
 * @ORM\Entity(repositoryClass="App\Repository\TestRepository")
 * @Vich\Uploadable
 */
class Test
{
/**
 * @ORM\Id()
 * @ORM\GeneratedValue()
 * @ORM\Column(type="integer")
 */
private $id;

/**
 * @ORM\Column(type="string", length=255)
 */
private $imageName;

/**
 * @Vich\UploadableField(mapping="test_images", fileNameProperty="imageName")
 * @var File
 */
private $imageFile;

/**
 * @ORM\Column(type="datetime")
 * @var \DateTime
 */
private $updatedAt;

public function getId()
{
    return $this->id;
}

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

public function setImageName(string $image)
{
    $this->imageName = $image;

    return $this;
}

public function setImageFile(File $image = null)
{
    $this->imageFile = $image;

    if ($image) {
        $this->updatedAt = new \DateTime('now');
    }
}

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

public function getUpdatedAt()
{
    return $this->updatedAt;
}

public function setUpdatedAt(\DateTime $datetime)
{
    $this->updatedAt = $datetime;

    return $this;
}
}

Mine easy admin yaml config

easy_admin:
    entities:
        Test:
            class: App\Entity\Test
            form:
                fields:
                    - { property: 'imageFile', type: 'vich_image' }
            # ...
            list:
                fields:
                    - { property: 'image', type: 'image', base_path: '%app.path.banner_images%' }
            # ...
            show:
                fields:
                    - { property: 'image', type: 'image', base_path: '%app.path.banner_images%' }
garak commented 5 years ago

Try with

public function setImageName(?string $image)
{
    $this->imageName = $image;

    return $this;
}
serhiobb commented 5 years ago

thanks, that helps