模型的 save() 方法无法使用队列?

目的:我想要异步 save()

Controller:

        $model = User::find(1);
        dispatch(new ModelSave($model));

Job

    public function handle()
    {
        //
        $this->model->save();
    }

Job exception

Illuminate\Database\Eloquent\ModelNotFoundException: No query results for model [App\Models\Device]. in E:\server\wwwroot\xiaohoumengqi\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Builder.php:452
Stack trace:
#0 E:\server\wwwroot\xiaohoumengqi\vendor\laravel\framework\src\Illuminate\Queue\SerializesAndRestoresModelIdentifiers.php(85): Illuminate\Database\Eloquent\Builder->firstOrFail()
#1 E:\server\wwwroot\xiaohoumengqi\vendor\laravel\framework\src\Illuminate\Queue\SerializesAndRestoresModelIdentifiers.php(55): App\Jobs\ModelSave->restoreModel(Object(Illuminate\Contracts\Database\ModelIdentifier))
#2 E:\server\wwwroot\xiaohoumengqi\vendor\laravel\framework\src\Illuminate\Queue\SerializesModels.php(45): App\Jobs\ModelSave->getRestoredPropertyValue(Object(Illuminate\Contracts\Database\ModelIdentifier))
#3 [internal function]: App\Jobs\ModelSave->__wakeup()
#4 E:\server\wwwroot\xiaohoumengqi\vendor\laravel\framework\src\Illuminate\Queue\CallQueuedHandler.php(42): unserialize('O:18:"App\\Jobs\\...')
#5 E:\server\wwwroot\xiaohoumengqi\vendor\laravel\framework\src\Illuminate\Queue\Jobs\Job.php(83): Illuminate\Queue\CallQueuedHandler->call(Object(Illuminate\Queue\Jobs\DatabaseJob), Array)
#6 E:\server\wwwroot\xiaohoumengqi\vendor\laravel\framework\src\Illuminate\Queue\Worker.php(327): Illuminate\Queue\Jobs\Job->fire()
#7 E:\server\wwwroot\xiaohoumengqi\vendor\laravel\framework\src\Illuminate\Queue\Worker.php(277): Illuminate\Queue\Worker->process('database', Object(Illuminate\Queue\Jobs\DatabaseJob), Object(Illuminate\Queue\WorkerOptions))
#8 E:\server\wwwroot\xiaohoumengqi\vendor\laravel\framework\src\Illuminate\Queue\Worker.php(118): Illuminate\Queue\Worker->runJob(Object(Illuminate\Queue\Jobs\DatabaseJob), 'database', Object(Illuminate\Queue\WorkerOptions))
#9 E:\server\wwwroot\xiaohoumengqi\vendor\laravel\framework\src\Illuminate\Queue\Console\WorkCommand.php(102): Illuminate\Queue\Worker->daemon('database', 'modelsave', Object(Illuminate\Queue\WorkerOptions))
#10 E:\server\wwwroot\xiaohoumengqi\vendor\laravel\framework\src\Illuminate\Queue\Console\WorkCommand.php(86): Illuminate\Queue\Console\WorkCommand->runWorker('database', 'modelsave')
#11 [internal function]: Illuminate\Queue\Console\WorkCommand->handle()
#12 E:\server\wwwroot\xiaohoumengqi\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php(29): call_user_func_array(Array, Array)
#13 E:\server\wwwroot\xiaohoumengqi\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php(87): Illuminate\Container\BoundMethod::Illuminate\Container\{closure}()
#14 E:\server\wwwroot\xiaohoumengqi\vendor\laravel\framework\src\Illuminate\Container\BoundMethod.php(31): Illuminate\Container\BoundMethod::callBoundMethod(Object(Illuminate\Foundation\Application), Array, Object(Closure))
#15 E:\server\wwwroot\xiaohoumengqi\vendor\laravel\framework\src\Illuminate\Container\Container.php(572): Illuminate\Container\BoundMethod::call(Object(Illuminate\Foundation\Application), Array, Array, NULL)
#16 E:\server\wwwroot\xiaohoumengqi\vendor\laravel\framework\src\Illuminate\Console\Command.php(183): Illuminate\Container\Container->call(Array)
#17 E:\server\wwwroot\xiaohoumengqi\vendor\symfony\console\Command\Command.php(255): Illuminate\Console\Command->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle))
#18 E:\server\wwwroot\xiaohoumengqi\vendor\laravel\framework\src\Illuminate\Console\Command.php(170): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Illuminate\Console\OutputStyle))
#19 E:\server\wwwroot\xiaohoumengqi\vendor\symfony\console\Application.php(1009): Illuminate\Console\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#20 E:\server\wwwroot\xiaohoumengqi\vendor\symfony\console\Application.php(273): Symfony\Component\Console\Application->doRunCommand(Object(Illuminate\Queue\Console\WorkCommand), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#21 E:\server\wwwroot\xiaohoumengqi\vendor\symfony\console\Application.php(149): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#22 E:\server\wwwroot\xiaohoumengqi\vendor\laravel\framework\src\Illuminate\Console\Application.php(89): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#23 E:\server\wwwroot\xiaohoumengqi\vendor\laravel\framework\src\Illuminate\Foundation\Console\Kernel.php(122): Illuminate\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#24 E:\server\wwwroot\xiaohoumengqi\artisan(37): Illuminate\Foundation\Console\Kernel->handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#25 {main}

对模型原理不懂,难道要在队列 重新new或者 重新查找,重新赋值,再重新save吗?

本作品采用《CC 协议》,转载必须注明作者和本文链接
reading
白小二
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 14

file

见:『找不到』异常

你调用了firstOrFail findOrFail 之类的

错误的代码应该不是你贴出的代码。

3年前
白小二

@lyxxxh 这是队列失败记录的日志,这就是全部的代码了,就很普通的 save ,只是我想用队列异步处理

3年前

看错误信息,你的findFirst就找不到model,model不存在,后面怎么能save

3年前

因为 Model 在放队列的时候自动序列化了,所以不能在队列里进行保存等操作。

你试试把队列的 SerializesModels 删掉试试呢。

3年前
nff93

队列每次都会重新从数据库查找,所以你需要每次在队列里赋值然后保存

3年前

可能开启了事务没提交。

3年前
白小二

@taohua 就代码所示的一个 save() ,没有其它操作了

3年前
  • 我尝试了一下,发现是可以的,不知道能不能帮助到你

控制器

public function modelSave()
{
       $model = Order::find(1);
       ModelSave::dispatch($model);
 }

Job

<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use App\Model\Order;

class ModelSave implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public $model;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(Order $model)
    {
        $this->model = $model;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $this->model->name = 'xiaohuasheng';
        $this->model->save();
    }
}

file

3年前
白小二

@xiaohuasheng 有尝试过 new Order; 吗?我是 new 的时候报这个错,即使是在队列里面新增都不行, 控制器

    public function index(Request $request)
    {
        $data = [
            'username'=>'test'.time(),
            'password'=>bcrypt('123'),
            'api_token'=>md5(uniqid('auth').time()),
        ];
        $model = new Test();
        $model->username = $data['username'];
        $model->password = $data['password'];
        $model->api_token = $data['api_token'];
        TestJob::dispatch($model);
    }

job

//获取表明
   $table = $this->model->getTable();
     //获取数据
        $data = $this->model->toArray();
       //有id则更新
        if($id = $this->model->id){
            Db::table($table)->find($id)->update($data);
            return;
        }        
//无id新增
$model = Db::table($table)->create($data);
3年前

@白小二 可能是因为这句话吧,一个新new的模型无法放入构造函数里 file

file

3年前
白小二

@xiaohuasheng 那是不是意味着我得采用传 类名+创建|更新+数据 这种形式了,按照这译文,构造函数不能接收新的模型啊

3年前
白小二

@24K大白羊 大神,终于搞定了,我太迟钝了,在看了 @xiaohuasheng 的回答,我才知道你说的是啥 :joy:

3年前
白小二

@LiamHao 感谢

3年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!