douao1959 2018-09-18 20:03
浏览 59

Oneup + Dropzone如何在表单提交后分配Image Entity值

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.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
    • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
    • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
    • ¥20 腾讯企业邮箱邮件可以恢复么
    • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
    • ¥15 错误 LNK2001 无法解析的外部符号
    • ¥50 安装pyaudiokits失败
    • ¥15 计组这些题应该咋做呀
    • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
    • ¥15 让node服务器有自动加载文件的功能