经过我的不屑努力和研究终于得到解决方案
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", //<==注意这里要替换
],