Create form for Uploadable Doctrine Extension

690 Views Asked by At

I'd like to use Uploadable to save some images (i.e. profile picture for users). I'm using many other Doctrine Extensions already (softdeletable, timestampable, blameable, etc.) so I thought it would be nice to use this one as well.

However, I don't know how to set up my Forms. The StofDoctrineExtensionsBundle documentation gives this example:

$document = new Document();
$form = $this->createFormBuilder($document)
    ->add('name')
    ->add('myFile')
    ->getForm()
;

//(...)

$uploadableManager->markEntityToUpload($document, $document->getMyFile());

In this example, is name the name of the Document or the name of the file?

Atlantic18/DoctrineExtensions's documentation adds a path, name, mimeType and size to an entity, to there is no myFile attribute.

Can anybody explain how to set up a Form for a Uploadable Doctrine entity? I couldn't find any documentation or good example that helped me further.

1

There are 1 best solutions below

0
On BEST ANSWER

Entity

Like you've discovered, the documentation of DoctrineExtensions itself sheds some light on how to configure the Uploadable extension to use your entity.

Mainly by adding the @Gedmo\Uploadable annotation to your entity and @Gedmo\UploadableFilePath to the property that will contain the file path ($filePath for example).

Form

With the ->add() method of the form-builder you add fields to the form. The first parameter specifies the property-name of the corresponding entity. So ->add('myFile') would add a field for the property $myFile.

What you need to do is add a (file) field to the form for the property that will contain the file path ($filePath for example), and mark that property:

$form = $this->createFormBuilder($entity)
    ->add('filePath');

$uploadableManager->markEntityToUpload($entity, $entity->getFilePath());

In other words: myFile in your example should be replaced with filePath in my example, and whatever the actual property is in your real code.