【问题标题】:Passing object on Queue as data laravel 4 / PHP将队列上的对象作为数据 laravel 4 / PHP
【发布时间】:2014-07-08 01:25:56
【问题描述】:

我创建了一个类,负责在上传图像后调整图像大小。此类将与队列遮阳板一起使用。在我的开发项目中有这个类,队列的默认设置设置为sync

队列工作正常,但出乎意料的大问题是在数据数组上传递一个对象,当我在队列的类处理程序中获取它时,因为一个空的array

这种"serialization" 的对象打破了我实现这个很棒的类的所有逻辑。

我想问一下这种行为是否正常,如果是,如何解决在我的队列类中将对象作为数据传递?

这就是我在 handlerQueue 类中传递对象的方式

$file = Input::file('file');
$image = new Image($file);
Queue::push('HandlerQueue',['image' => $image]);


class HandlerQueue
{
   public function fire($job,$data)
   {
       dd($data['image']); // Empty array :(
   }

}

任何帮助将不胜感激。

【问题讨论】:

    标签: laravel laravel-4 queue


    【解决方案1】:

    你不能在没有序列化的情况下将对象传递到队列中。

    你可以做的是传递一个对对象的引用,然后再次调用它。像这样(伪代码):

    $file = Input::file('file');
    $image = new Image($file);
    $image_id = save $file and get ID  // save reference
    Queue::push('HandlerQueue',['image_id' => $image_id]);
    
    
    class HandlerQueue
    {
       public function fire($job,$data)
       {
           $image = new Image($data['image_id']);  // use the reference and recreate the object
       }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-22
      • 2013-10-20
      • 2019-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多