Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 990
Nicki Avatar asked Mar 02 '16 14:03

Nicki


2 Answers

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.
like image 81
genesst Avatar answered Nov 11 '22 15:11

genesst


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) {
        $entity->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.

like image 2
Matt Raines Avatar answered Nov 11 '22 17:11

Matt Raines