[英]Getting current instance of entity and passing it to a listener in Symfony
我有一個用於將值放入兩個不同實體中的表格。 一個實體是listing
表,另一個是images
表。 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
。
在上載中包含列表ID。
如果dropzone是單獨的表單,請添加具有ID的隱藏輸入。 您可以在模板中渲染該值,以使用JS加載時填充它。
如果dropzone是通過JS初始化的,則將ID添加到params選項中。
現在, 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>
然后在您的UploadListener
類中獲取清單ID,如下所示是請求對象:
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();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.