簡體   English   中英

獲取當前實體實例並將其傳遞給Symfony中的偵聽器

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM