SQLSTATE[HY000]: General error: 2014 错误如何处理

本地服务器,运行laravel项目,正常运行、生产服务器提示错误.
这里的sql代码是debug里面输出的

update `cy_product_type` set `parentid` = '3', `tree` = '0-1-3-551' where `id` = '551'
update cy_product_type set tree = CONCAT('0-1-3-551-',id) where tree like '0-1-550-551-%'

请问这个是什么错误?

SQLSTATE[HY000]: General error: 2014 Cannot execute queries while other unbuffered queries are active.  Consider using PDOStatement::fetchAll().  Alternatively, if your code is only ever going to run against mysql, you may enable query buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute. (SQL: update cy_product_type set tree = CONCAT('0-1-3-551-',id) where tree like '0-1-550-551-%')
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
最佳答案

@LiamHao 没有并发,我只是在mode中updated事件执行更新代码。目前不知道如何调试,请指点下。
而且本地开发环境正常,生产环境报错。。

file

最后自己重写了一下代码,解决了。原因不清楚..

解决办法:把模型事件updated中的代码改为:

        static::updated(function ($model) {
            $dirty    = $model->getDirty();
            $original = $model->getRawOriginal();

            // 移动节点
            if (isset($dirty['parentid'])) {
                DB::table('product_type')->where('tree', 'like', $original['tree'] . '-%')->update(['tree' => DB::raw("CONCAT('{$model->tree}-',id)")]);
                $child = DB::table('product_type')->where('parentid', $original['parentid'])->count();
                if (!$child) {
                    DB::table('product_type')->where('id', $original['parentid'])->update(['child' => 0]);
                }
            }
        });
2年前 评论
LiamHao 2年前
讨论数量: 2

并发的情况下,一个查询还没有结束,另一个查询开始。尝试将MYSQL_ATTR_USE_BUFFERED_QUERY设为 true
config/database.php中:

    'connections' => [
        'mysql' => [
            ...
            'options' => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
                // 加在这里
                PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
            ]) : [],
        ],
    ],
2年前 评论
91it (楼主) 2年前

@LiamHao 没有并发,我只是在mode中updated事件执行更新代码。目前不知道如何调试,请指点下。
而且本地开发环境正常,生产环境报错。。

file

最后自己重写了一下代码,解决了。原因不清楚..

解决办法:把模型事件updated中的代码改为:

        static::updated(function ($model) {
            $dirty    = $model->getDirty();
            $original = $model->getRawOriginal();

            // 移动节点
            if (isset($dirty['parentid'])) {
                DB::table('product_type')->where('tree', 'like', $original['tree'] . '-%')->update(['tree' => DB::raw("CONCAT('{$model->tree}-',id)")]);
                $child = DB::table('product_type')->where('parentid', $original['parentid'])->count();
                if (!$child) {
                    DB::table('product_type')->where('id', $original['parentid'])->update(['child' => 0]);
                }
            }
        });
2年前 评论
LiamHao 2年前

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