获取实体的当前实例并将其传递给Symfony中的侦听器
我有一个表单,用于将值放入两个不同的实体中。一个实体是获取实体的当前实例并将其传递给Symfony中的侦听器,symfony,listener,dropzone,oneupuploaderbundle,Symfony,Listener,Dropzone,Oneupuploaderbundle,我有一个表单,用于将值放入两个不同的实体中。一个实体是列表表,另一个是图像表。images表由侦听dropzonePostPersistent的侦听器处理。每次将图像拖放到区域中时,它都会添加到数据库中。有一段时间我遇到了一个问题,如果用户只是第一次创建表单,列表就不存在,因此没有id来绑定我解决的图像实体 现在,每次拖放图像时,我都会尝试获取用户正在查看表单的当前列表实体的id,并将其用作图像实体中列表id的值 上传侦听器 <?php namespace DirectoryPlatfor
列表
表,另一个是图像
表。images表由侦听dropzonePostPersistent
的侦听器处理。每次将图像拖放到区域中时,它都会添加到数据库中。有一段时间我遇到了一个问题,如果用户只是第一次创建表单,列表就不存在,因此没有id
来绑定我解决的图像
实体
现在,每次拖放图像时,我都会尝试获取用户正在查看表单的当前列表
实体的id,并将其用作图像实体中列表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();
}
}
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 }
我的目的是在图像上传到db时将列表id添加到图像中。image
实体中的listing\u id
绑定到列表实体的id
,但是我没有办法从侦听器中获取表单的当前实例
我的问题是,如何获取用户当前在
UploadListener
中查看的实体列表
,以便我可以使用它的id
,并将其设置为上传图像的列表id
。将列表id包括在上传中
如果dropzone是一个单独的表单,请添加一个id为的隐藏输入。您可以在模板中呈现值,即使用JS加载时填充它
如果dropzone是通过JS初始化的,请将ID添加到选项中
现在,UploadListener
在请求中有了清单ID
至于在创建列表时包含图像,您可以在呈现创建表单之前生成ID,例如UUIDv4,将其设置为实体,现在它也呈现在表单中,可用于上载。在上载时包含列表ID 如果dropzone是一个单独的表单,请添加一个id为的隐藏输入。您可以在模板中呈现值,即使用JS加载时填充它 如果dropzone是通过JS初始化的,请将ID添加到选项中 现在,
UploadListener
在请求中有了清单ID
至于在创建列表时包含图像,您可以在呈现创建表单之前生成ID,例如UUIDv4,将其设置为实体,现在它也呈现在表单中,可用于上载。在您的小枝中,您应该使用javascript脚本配置dropzone,以向您的请求传递一些额外的参数:
<script>
Dropzone.options.yourFormId = {
params: {
listing: "{{ listing.id }}" // if you pass listing as a variable to your template
// or listing: "{{ form.vars.value.id }}" if listing is the underlying object of your form
}
};
</script>
在您的小树枝中,您应该使用javascript脚本来配置dropzone,以便向您的请求传递一些额外的参数:
<script>
Dropzone.options.yourFormId = {
params: {
listing: "{{ listing.id }}" // if you pass listing as a variable to your template
// or listing: "{{ form.vars.value.id }}" if listing is the underlying object of your form
}
};
</script>
你的问题到底是什么?你被困在哪里?你有什么错误?到目前为止你试过什么?你的问题是什么?你被困在哪里?你有什么错误?到目前为止你试过什么?
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();
$object = new MotorsAdsFile();
$object->setImageName($file->getPathName());
// Get the listing parameter
$request = $event->getRequest();
$listingId = $request->get('listing');
// //
$object->setListing($listingId);
$this->manager->persist($object);
$this->manager->flush();
}
}