博文 > yii2 console MySQL server

yii2 console MySQL server

html10002018-09-18 15:26:00

yii2 console MySQL server has gone away

经过我的不屑努力和研究终于得到解决方案

<?php
namespace common\lib;
class Command extends \yii\db\Command{
    public function execute()
    {
        try {
            return parent::execute();
        } catch (\yii\db\Exception $e) {
            if ($e->errorInfo[1] == 2006 || $e->errorInfo[1] == 2013) {
                $this->db->close();
                $this->db->open();
                $this->pdoStatement = null ;
                return parent::execute();
            }else{
                throw $e;
            }
        }
    }

    protected function queryInternal($method, $fetchMode = null){
        try {
            return parent::queryInternal($method, $fetchMode);
        } catch (\yii\db\Exception $e) {
            if ($e->errorInfo[1] == 2006 || $e->errorInfo[1] == 2013) {
                $this->db->close();
                $this->db->open();
                $this->pdoStatement = null ;

                return parent::queryInternal($method, $fetchMode);
            }else{
                throw $e;
            }
        }
    }


}
'db' => [
            'class' => 'yii\db\Connection',
            'dsn' => 'mysql:host='.$db_config['host'].';dbname='.$db_config['dbname'],
            'username' => $db_config['username'],
            'password' => $db_config['password'],
            'charset' => 'utf8',
            'commandClass'=>"\\common\\lib\\Command", //<==注意这里要替换
],

<!--more-->

全部评论1

  • 小汪汪2018/06/06

    适合具备一定的Javascript基础知识的读者阅读,也适合从事程序设计工作并想要深入探索Javascript语言的读者阅读。 您不需要具备任何的Javascript基础知识及项目经验,通过学习这本书。 将会在面试有关Javascript程序设计的职位是游刃有余