启动创建事件时没有模型查询结果

时间:2018-07-31 07:31:49

标签: laravel laravel-5 eloquent

我有一个具有boot函数的模型,该模型具有如下所示的created事件。

但是,有时(并非总是)我在No query results for model上得到ProcessAddressRefine这是一份工作。据我了解,created事件应在创建记录后发生,因此除非创建后立即将其删除,否则没有查询结果的方法。我还想知道查看数据库记录,ProcessAddressRefine作业是否正确执行。

在这种情况下会有什么问题?

任何建议或建议将不胜感激。谢谢。

模型

public static function boot()
{
    parent::boot();

    self::created(function ($model) {
        if (!$model->lat || !$model->lng) {
            ProcessAddressRefine::dispatch($model);
        }
    });
}

工作

class ProcessAddressRefine implements ShouldQueue
{
    use Dispatchable, SerializesModels;

    private $place;

    public function __construct($place)
    {
        $this->place = $place;
    }

    public function handle()
    {
        if ($this->place->addressRefine()) {
            $this->place->save();
        }
    }
}

额外

public function addressRefine()
{
    $helper = new MapHelper();
    $coordinate = $helper->addressToCoordinate($geo_code_address);
    if ($coordinate !== false) {
        $this->lat = $coordinate['lat'];
        $this->lng = $coordinate['lng'];
        return true;
    } else {
        return false;
    }
}

2 个答案:

答案 0 :(得分:0)

假设作业已排队,则很有可能创建了模型,然后您调度了该作业,然后删除了模型,然后真正执行了作业,您会收到此消息,因为模型不再存在。

答案 1 :(得分:0)

这是由于创建订单记录时的DB::transaction