I have many ORM entities which I want to link them to their corresponding files. Example: the entity Motors with MotorsFile, Houses with HousesFile, ...
These ORM entities can be easily definied. This is not a problem. But, my problem is: "Is it possible to define many ORM entities such us MotorsFile and HousesFile while using OneupUploaderBundle?"
I asked this question because processing these uploaded file with doctrine needs creating an event listener to PostUploadEvent
and PostPersistEvent
. The event listener would be something like that:
<?php
namespace Acme\HelloBundle\EventListener;
use Oneup\UploaderBundle\Event\PostPersistEvent;
use Minn\AdsBundle\Entity\MotorsAdsFile;
class UploadListener
{
protected $manager;
public function __construct(EntityManager $manager)
{
$this->manager = $manager;
}
public function onUpload(PostPersistEvent $event)
{
$file = $event->getFile();
$object = new MotorsFile();
$object->setFilename($file->getPathName());
$this->manager->persist($object);
$this->manager->flush();
}
}
But, this code will allow me only to persist one entity (for this example MotorsFile). So, is it possible to specify the uploaded file is corresponding to which entity?
Thanks...
You basically have two options.
Given you use different mappings for the different file uploads you can use generic events that will be dispatched for each mapping.
After each upload not only the listeners for the oneup_uploader.post_upload
will be executed, but also the listeners for the special event oneup_uploader.post_upload.{mapping}
, where {mapping}
is the configured name in your config.yml
.
Let's say you have a configuration similar to this one:
oneup_uploader:
mappings:
motors:
frontend: blueimp
storage:
directory: %kernel.root_dir%/../web/uploads/motors
houses:
frontend: blueimp
storage:
directory: %kernel.root_dir%/../web/uploads/houses
After uploading a file to the motors
mapping, the generic event oneup_uploader.post_upload.motors
will be dispatched. The same goes for the houses
mapping.
If you don't want to use different upload handlers, you could also check for the type
.
<?php
namespace Acme\HelloBundle\EventListener;
use Oneup\UploaderBundle\Event\PostPersistEvent;
use Minn\AdsBundle\Entity\MotorsAdsFile;
class UploadListener
{
public function onUpload(PostPersistEvent $event)
{
$type = $this->getType();
}
}
$type
will be either motors
or houses
, depending with which mapping configuration the file was uploaded to the server.