【发布时间】:2018-09-20 16:25:06
【问题描述】:
我有一个表格,用于将值放在两个不同的实体中。一个实体是listing 表,另一个是images 表。图像表由侦听 dropzone PostPersistEvent 的侦听器处理。每次将图像拖放到区域中时,都会将其添加到数据库中。有一段时间我遇到了一个问题,如果用户只是第一次创建表单,列表不存在,所以没有id 来绑定我解决的image 实体。
现在我正在尝试,每次拖放图像时,获取用户正在查看表单的当前 listing 实体的 ID,并将其用作图像中 listing_id 的值实体。
上传监听器
<?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;
}
// If I could pass a current instance of the currently viewed Listing entity here, that would be ideal
public function onUpload(PostPersistEvent $event)
{
$file = $event->getFile();
// images entity
$object = new MotorsAdsFile();
$object->setImageName($file->getPathName());
// I'd want to set the listing_id of MotorsAdsFile to the id of the currently viewed listing here
// $object->setListing($listing->getId());
$this->manager->persist($object);
$this->manager->flush();
}
}
MotorsAdsFile(图像实体)
/**
* @param Listing $listing
*/
public function setListing($listing)
{
$this->listing = $listing;
}
services.yml
directory_platform.upload_listener:
class: DirectoryPlatform\AppBundle\EventListener\UploadListener
arguments: ["@doctrine.orm.entity_manager"]
tags:
- { name: kernel.event_listener, event: oneup_uploader.post_persist, method: onUpload }
我的意图是在图像上传到数据库时将列表 ID 添加到图像中。 image 实体中的 listing_id 与列表实体的 id 相关联,但我无法从侦听器中获取表单的当前实例
我的问题是,如何获取用户当前在UploadListener 中查看的实体listing 的实例,以便我可以使用它的id 并将其设置为上传图像的listing_id .
【问题讨论】:
-
您的问题到底是什么?你被困在哪里了?你得到什么错误?到目前为止,您尝试过什么?
标签: symfony listener dropzone oneupuploaderbundle