I have an entity which is MotorsAdsFile
, I use an event listener to add the image to the table after it's been dragged and dropped
Upload Listener
<?php
namespace DirectoryPlatform\AppBundle\EventListener;
use Doctrine\Common\Persistence\ObjectManager;
use Oneup\UploaderBundle\Event\PostPersistEvent;
use DirectoryPlatform\AppBundle\Entity\MotorsAdsFile;
use Symfony\Bundle\FrameworkBundle\Routing\Router;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use FOS\UserBundle\Event\FilterUserResponseEvent;
use FOS\UserBundle\FOSUserEvents;
class UploadListener
{
protected $manager;
public function __construct(ObjectManager $manager)
{
$this->manager = $manager;
}
public function onUpload(PostPersistEvent $event)
{
$file = $event->getFile();
$object = new MotorsAdsFile();
// $object = new Image();
$object->setImageName($file->getPathName());
$this->manager->persist($object);
$this->manager->flush();
}
}
The image is dragged and dropped, the thumbnail preview shows up, at this time the image file path is added to the motorsadsfile table and the image is also moved to the gallery
folder. This works just fine except that I need to assign the image a listing_id
which is only generated after the form has been completed and submitted.
Listing Type
<?php
namespace DirectoryPlatform\FrontBundle\Form\Type;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\MoneyType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use DirectoryPlatform\FrontBundle\Form\Type\DropZoneType;
use AdamQuaile\Bundle\FieldsetBundle\Form\FieldsetType;
use DirectoryPlatform\AppBundle\Entity\Listing;
class ListingType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', TextType::class)
->add('images', FormType::class, [
'label' => 'Drop files here',
'attr' => [
'class' => 'dropzone',
'action' => '/_uploader/gallery/upload',
]
])
->add('header', ChoiceType::class, [
'expanded' => true,
'choices' => [
'None' => Listing::HEADER_NONE,
'Single Image' => Listing::HEADER_SINGLE_IMAGE,
'Gallery' => Listing::HEADER_GALLERY,
'Map' => Listing::HEADER_MAP,
]
])
->add('isAuction', ChoiceType::class, [
'label' => 'Is this an auction?',
'required' => true,
'choices' => [
'Yes' => true,
'No' => false,
]
])
->add('website', TextType::class, [
'required' => false,
'label' => 'If the auction is online, please provide a website.'
])
->add('slug', TextType::class)
->add('description', TextareaType::class, [
'required' => false,
'attr' => ['class' => 'wysiwyg', 'rows' => 5],
])
->add('address', TextareaType::class, [
'required' => false,
'attr' => ['rows' => 3]
])
->add('price', MoneyType::class, [
'currency' => $options['currency'],
'required' => false,
])
->add('acres', IntegerType::class, [
'label' => 'Acres',
'required' => true,
])
->add('videoYoutube', TextType::class, [
'required' => false,
])
->add('openingHours', TextareaType::class, [
'required' => false,
'attr' => [
'rows' => 7,
]
])
->add('geolocation', FieldsetType::class, [
'label' => false,
'legend' => 'Geolocation',
'fields' => function (FormBuilderInterface $builder) use ($options) {
$builder->add('search', TextType::class, [
'mapped' => true,
'required' => false,
])
->add('latitude', NumberType::class, [
'required' => false,
])
->add('longitude', NumberType::class, [
'required' => false,
]);
}
])
->add('taxonomies', FieldsetType::class, [
'label' => false,
'legend' => 'Taxonomies',
'fields' => function (FormBuilderInterface $builder) use ($options) {
$builder->add('category', EntityType::class, [
'class' => 'AppBundle:Category',
'required' => false,
'choices' => $options['hierarchy_categories']->tree(),
'choice_label' => function ($category) use ($options) {
return $options['hierarchy_categories']->getName($category);
},
])
->add('location', EntityType::class, [
'class' => 'AppBundle:Location',
'required' => false,
'choice_label' => function ($location) use ($options) {
return $options['hierarchy_locations']->getName($location);
},
])
->add('amenities', EntityType::class, [
'class' => 'AppBundle:Amenity',
'multiple' => true,
'expanded' => true,
'required' => false,
]);
}
])
->add('save', SubmitType::class, [
'attr' => ['class' => 'btn-primary'],
]);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'currency' => 'USD',
'hierarchy_categories' => null,
'hierarchy_locations' => null,
'data_class' => 'DirectoryPlatform\AppBundle\Entity\Listing',
]);
}
}
Maybe there's an event I could listen for that coincides with successful form submission so that I could just add the listing_id
to the motorsadsfile
table after the listing has been created and actually has an id?
One way I could see maybe doing this is in dropzone.js setting it to populate a hidden field in the form with the file path so it can be used later but I'd like to know if you have any suggestions.
Image and Listing are separate entities which are join by the listing_id
from Image
and the id
from Listing
.