Symfony VichUploaderBundle: File name could not be generated

I am using VichUploader to upload files within a symfony project. In configuration i use (copied from documentation):

service: vich_uploader.namer_property
options: { property: 'slug'}

In my entity i generate the slugs automatically with Gedmo/Sluggable:

/**
 * @Gedmo\Slug(fields={"title"}, updatable=false)
 * @ORM\Column(type="string", length=100, nullable=false)
 */
protected $slug;

But when trying to save the entity i get the following error 500:

File name could not be generated: property slug is empty.

If i set the property to 'title' it works. Did i forget a configuration parameter or something else to get it working with the Gedmo slug?

I'm having the same issue at the moment, as a workaround, I've slightly changed the slug getter in the entity class:

use Gedmo\Sluggable\Util\Urlizer;

class Event
{
    // ...

    /**
     * @var string
     *
     * @Gedmo\Slug(fields={"name"})
     * @ORM\Column(name="slug", type="string", length=128, unique=true)
     */
    private $slug;

    // ...

    public function getSlug()
    {
        if (!$this->slug) {
            return Urlizer::urlize($this->getName());
        }

        return $this->slug;
    }
}

That did the trick.

Unfortunately, there're a couple of drawbacks:

  1. If you ever want to update sluggable behaviour in the annotation to include additional properties, you'll have to update the getter as well.
  2. This method lacks a check against the database: if there's already a record in the database with the same name urlizer in the getter won't be able to add an increment to the file name, previously saved file may be overwritten! As a workaround, you can add unique=true to sluggable properties.

add the generated id to the filename � Issue #14 � dustin10 , By now it's not possible to give the Document (or Entity) a filename with the generated id. This is dustin10 / VichUploaderBundle http://symfony.com/doc/ 2.0/cookbook/doctrine/file_uploads.html#using-the-id-as-the-filename @ dustin10 i also think there should be some standard generated PK namer. File name could not be generated: property slug is empty. If i set the property to 'title' it works. Did i forget a configuration parameter or something else to get it working with the Gedmo slug?

By default the doctrine extensions bundle does not attach any listener: http://symfony.com/doc/current/bundles/StofDoctrineExtensionsBundle/index.html#activate-the-extensions-you-want

You should configure it to get sluggable working:

stof_doctrine_extensions:
    orm:
        default:
            sluggable: true

How to Handle File Uploads with Doctrine (The Symfony CookBook), length=100, nullable=false) */ protected $slug; But when trying to save the entity i get the following error 500: File name could not be generated: property slug� A simple Symfony bundle to ease file uploads with ORM entities and ODM documents. - dustin10/VichUploaderBundle . ('File name could not be generated:

I was having the same problem uploading a document for which I needed the fileName to be the slug.

I was using Gedmo annotations to generate the slug, however this only triggers on flush and the vichUploader namer is triggered upon persist.

The easiest way for me to get this working was to not use the Gedmo\Sluggable annotation but rather create a prePersist listener on my Document object and use the Cocur\Slugify library.

So here is the code.

My Document Entity:

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;

/**
 * @ORM\Entity(repositoryClass="App\Repository\DocumentRepository")
 * @Vich\Uploadable
 * @ORM\EntityListeners({"App\Listeners\DocumentListener"})
 */
class Document
{
    use TimestampableEntity;

    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;


    /**
     * @ORM\Column(type="string", length=100, nullable=false)
     */
    private $fileName;

    /**
     * @Vich\UploadableField(mapping="document", fileNameProperty="fileName")
     * @var File
     */
    private $documentFile;

    /**
     * @ORM\Column(type="string", length=100, unique=true)
     */
    private $slug;

    /**
     */
    public function getDocumentFile(): ?File
    {
        return $this->documentFile;
    }

    /**
     * @param File $documentFile
     * @return Document
     * @throws \Exception
     */
    public function setDocumentFile(File $documentFile = null): Document
    {
        $this->documentFile = $documentFile;
        if($documentFile){
            $this->updatedAt = new \DateTimeImmutable();
        }

        return $this;
    }

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

    public function getSlug(): ?string
    {
        return $this->slug;
    }

    public function setSlug(string $slug): self
    {
        $this->slug = $slug;

        return $this;
    }

    /**
     * @return mixed
     */
    public function getFileName()
    {
        return $this->fileName;
    }

    /**
     * @param mixed $fileName
     */
    public function setFileName($fileName): void
    {
        $this->fileName = $fileName;
    }
}

And the listener :

namespace App\Listeners;

use App\Entity\Document;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Cocur\Slugify\Slugify;

class DocumentListener
{
    public function prePersist(Document $document, LifecycleEventArgs $args)
    {
        $slugify = new Slugify();
        if(!empty($document->getDocumentFile())){
            $originalName = pathinfo($document->getDocumentFile()->getClientOriginalName(), PATHINFO_FILENAME);
            $slug = $slugify->slugify($originalName);
            $document->setSlug($slug);
        }
    }
}

So far I have not had any problems.

Let me know if this works for you

How to Upload Files (Symfony 2.8 Docs), File name could not be generated: property slug is empty. If i set the property to ' title' it works. Did i forget a configuration parameter or something else to get it� If you use annotations to configure VichUploaderBundle, you probably just forgot to add @Vich\Uploadable on top of your entity. If you don't use annotations, check that the configuration files are in the right place. In both cases, clearing the cache can also solve the issue. My configuration is with yml

VichUploader listens to the prePersist and preUpdate events, whereas Sluggable listens to the onFlush event. Because prePersist and preUpdate are called before onFlush, it isn't possible to do this purely using configuration.

However, if your file field is nullable, you can work around it by changing your controller code. When you receive the submitted form with the file, remove the file, save the entity, then re-add the file and save the entity again. On the second save, the slug will already be set, so VichUploader will be able to save the file fine.

if ($form->isSubmitted() && $form->isValid()) {
    if ($file = $entity->getFile()) {
        $entity->setFile(null);
    }

    $em = $this->getDoctrine()->getManager();
    $em->persist($entity);
    $em->flush();

    if ($file) {
        $document->setFile($file);
        $em->persist($entity);
        $em->flush();
    }

    // ...
}

This only works when adding a new file. If you subsequently change the slug and resave the entity without uploading a new file, the filename is not updated.

How to Upload Files (Symfony 3.3 Docs), Basic Setup; Using Lifecycle Callbacks; Using the id as the Filename In this case, as your entity is created, updated, and removed from Doctrine, the file uploading and If you have not done so already, you should probably read the file type� Questions: That's it, I have a problem with the photo upload on a form. I can save my form with Do

Generate PDF and video thumbnails with VichUploader on Symfony , How to Upload Files: Note Instead of handling file uploading yourself, you may consider using the VichUploaderBundle community bundle. WARNING: You are browsing the documentation for Symfony 2.8 which is not of the file names generated by // uniqid(), which is based on timestamps return md5(uniqid()); } }. hi, i'm try your bundle, but i don't understand how to use custom namer. Can you explain how to implement a custom namer , or show an example for a custom namer. thanks for help. Sorry for

Handling File Upload, WARNING: You are browsing the documentation for Symfony 3.3 which is not file uploading yourself, you may consider using the VichUploaderBundle of the file names generated by // uniqid(), which is based on timestamps return� The UploadedFile class provides methods to get the original file extension (getExtension()), the original file size (getClientSize()) and the original file name (getClientOriginalName()). However, they are considered not safe because a malicious user could tamper that information.

vich/uploader-bundle, minMessage = "Your file name must be at least {{ limit }} characters long", * maxMessage = "Your file name cannot be longer than {{ limit }}� Note. Instead of handling file uploading yourself, you may consider using the VichUploaderBundle community bundle. This bundle provides all the common operations (such as file renaming, saving and deleting) and it’s tightly integrated with Doctrine ORM, MongoDB ODM, PHPCR ODM and Propel.

Comments
  • I guess the reason is that both bundles use the flush event to operate and are executing in wrong order...
  • But are there chances to configure that?
  • @Nicki have you found how to do ? I just got the same issue - I changed the namer for vich_uploader.namer_uniqid as a quick fix but this is not what I want.
  • The slug is generated, but it seems that it is generated after VichUploader tries to access it.
  • @Nicki and you have the config above in your config.yml?
  • Not the exact configuration, since i installed Gedmo without the StofBundle. But the listener is configured and slugs are generated.