Reputation: 23
My problem is to retrieve metadata before uploading the file.
My config file:
vich_uploader:
db_driver: orm
mappings:
media:
uri_prefix: /uploads/
upload_destination: '%kernel.root_dir%/../web/uploads'
inject_on_load: false
delete_on_update: true
delete_on_remove: true
I have an entity MEDIA :
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
**
* @ORM\Entity
* @Vich\Uploadable
*/
class Media
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
*
* @Vich\UploadableField(mapping="media", fileNameProperty="fileName",originalName="originalFileName")
*
* @var File
*/
private $file;
/**
* @ORM\Column(type="string", length=50)
*/
private $fileName;
/**
* @ORM\Column(type="string", length=255, nullable=false)
*/
private $originalFileName;
/**
* @ORM\Column(type="datetime")
*/
private $updatedAt;
/**
* @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $file
*
* @return Media
*/
public function setFile(File $file = null)
{
$this->file = $file;
if ($file) {
$this->updatedAt = new \DateTimeImmutable();
}
return $this;
}
/**
* @return File|null
*/
public function getFile()
{
return $this->file;
}
/**
* @param string $fileName
*
* @return Media
*/
public function setFileName($fileName)
{
$this->fileName = $fileName;
return $this;
}
/**
* @return string|null
*/
public function getFileName()
{
return $this->fileName;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set updatedAt
*
* @param \DateTime $updatedAt
*
* @return Media
*/
public function setUpdatedAt($updatedAt)
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* Get updatedAt
*
* @return \DateTime
*/
public function getUpdatedAt()
{
return $this->updatedAt;
}
/**
* Set originalFileName
*
* @param string $originalFileName
*
* @return Media
*/
public function setOriginalFileName($originalFileName)
{
$this->originalFileName = $originalFileName;
return $this;
}
/**
* Get originalFileName
*
* @return string
*/
public function getOriginalFileName()
{
return $this->originalFileName;
}
}
And here is my controller:
/**
* Creates a new media entity.
*
* @Route("/new", name="media_new")
* @Method({"GET", "POST"})
*
* @param Request $request
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
*/
public function newAction(Request $request)
{
$media = new Media();
$form = $this->createForm(MediaType::class, $media);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($media);
$em->flush();
return $this->redirectToRoute(
'media_list'
);
}
return $this->render(
'media/new.html.twig',
[
'media' => $media,
'form' => $form->createView(),
]
);
}
And my form:
<?php
/**
* Created by PhpStorm.
* User: rafael
* Date: 4/10/17
* Time: 12:46 PM
*/
namespace AppBundle\Form;
use AppBundle\Entity\Media;
use AppBundle\Entity\MediaDescriptionHelper;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class MediaType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('file', FileType::class)
->add('save', SubmitType::class, [
'attr' => ['class' => 'btn-primary btn-block']
]);
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(['data_class' => Media::class]);
}
}
The problem is with the mapping of these values :
@Vich\UploadableField(mapping="media", fileNameProperty="fileName",originalName="originalFileName")
When I submit my form these values are 'null' :
An exception occurred while executing 'INSERT INTO media (file_name, original_file_name, updated_at) VALUES (?, ?, ?)' with params ["get_image_resultat_sans_cache2.php.jpeg", null, "2017-04-12 10:11:56"]:
I have these issues with all parameters :
(https://github.com/dustin10/VichUploaderBundle/blob/master/Resources/doc/usage.md)
The UploadableField annotation has a few options. They are as follows:
mapping: required, the mapping name specified in the bundle configuration to use; fileNameProperty: required, the property that will contain the name of the uploaded file; size: the property that will contain the size in bytes of the uploaded file; mimeType: the property that will contain the mime type of the uploaded file; originalName: the property that will contain the origilal name of the uploaded file.
I don't see what I did wrong...
Here is my Media (entity) after the form is submitted :
Media {#403 ▼ -id: null -file: UploadedFile {#15 ▼ -test: false -originalName: "get_image_resultat_sans_cache2.php.jpeg" -mimeType: "image/jpeg" -size: 203751 -error: 0 path: "/tmp" filename: "php9xsTdF" basename: "php9xsTdF" pathname: "/tmp/php9xsTdF" extension: "" realPath: "/tmp/php9xsTdF" aTime: 2017-04-12 10:11:56 mTime: 2017-04-12 10:11:56 cTime: 2017-04-12 10:11:56 inode: 6160452 size: 203751 perms: 0100600 owner: 1000 group: 1000 type: "file" writable: true readable: true executable: false file: true dir: false link: false } -fileName: null -originalFileName: null
It seems that's a problem when set metadata before uploading the file...
Thanks a lot in advance...
Upvotes: 1
Views: 1603
Reputation: 36
Which version of VichUploaderBundle do you use?
The documentation for the annotations refers to the dev-master one, while the stable one (1.5.3) doesn't support annotation for metadata out of the box.
You can see that Vich\UploaderBundle\Mapping\Annotation\UploadableField.php in the 1.5.3 version handles only annotations 'mapping' and 'fileNameProperty'.
While in the dev-master, it handles those and size, mimeType and originalName.
Same thing with Vich\UploaderBundle\Metadata\Driver\AnnotationDriver
If you want to achieve this in the 1.5.3 version you need to create an eventListener.
Here are the event triggered by Vich : https://github.com/dustin10/VichUploaderBundle/blob/1.5.3/Event/Events.php
Upvotes: 2