Yii2 ActiveRecord连接OpenGauss提示表不存在table not exist

1.修改数据库连接信息

文件位置 config/db.php

添加默认Schema

php 复制代码
return [
    'class' => 'yii\db\Connection',
    'dsn' => 'pgsql:host=127.0.0.1;port=5432;dbname=postgres',
    'username' => 'postgres',
    'password' => 'Pass@123',
    'charset' => 'utf8',
    //'enableSchemaCache' => true,//表结构是否缓存
	//'schemaCacheDuration' => 86400,
	//'schemaCache' => 'cache',
	'schemaMap' => [
        'pgsql'=> [
          'class'=>'yii\db\pgsql\Schema',
          'defaultSchema' => 'postgres' //默认Schema
        ]
    ]
];

2.修改pgsql的Schema函数

文件位置 vendor/yiisoft/yii2/db/pgsql/Schema.php

修改函数 findColumns($table)

php 复制代码
protected function findColumns($table)
{
	$tableName = $this->db->quoteValue($table->name);
	$schemaName = $this->db->quoteValue($table->schemaName);
	$sql = "SELECT * FROM information_schema.columns WHERE table_schema='". $table->schemaName ."' and table_name = '". $table->name ."'";
	$columns = $this->db->createCommand($sql)->queryAll();
	if (empty($columns)) {
		return false;
	}
	$prksql = "SELECT tc.constraint_name, tc.table_name, kcu.column_name, ccu.table_name AS foreign_table_name, ccu.column_name AS foreign_column_name 
		FROM information_schema.table_constraints tc 
		JOIN information_schema.key_column_usage kcu ON tc.constraint_name = kcu.constraint_name 
		JOIN information_schema.constraint_column_usage ccu ON ccu.constraint_name = tc.constraint_name 
		WHERE tc.constraint_schema='". $table->schemaName ."' and constraint_type = 'PRIMARY KEY' AND tc.table_name='". $table->name ."'";
	$prkcolumns = $this->db->createCommand($prksql)->queryAll();
	foreach ($columns as $column) {
		if ($this->db->slavePdo->getAttribute(\PDO::ATTR_CASE) === \PDO::CASE_UPPER) {
			$column = array_change_key_case($column, CASE_LOWER);
		}
		if(stripos($column["column_default"], "nextval")!==false && stripos($column["column_default"], "_seq")!==false){
			$column["is_autoinc"] = 1;
		}else{
			$column["is_autoinc"] = 0;
		}
		$column["size"] = $column["character_maximum_length"];
		$column["is_pkey"] = 0;
		foreach($prkcolumns as $oneprkcol){
			if($oneprkcol["column_name"]==$column["column_name"]){
			   $column["is_pkey"] = 1;
			}
		}
		$column = $this->loadColumnSchema($column);
		$table->columns[$column->name] = $column;
		if ($column->isPrimaryKey) {
			$table->primaryKey[] = $column->name;
			if ($table->sequenceName === null && preg_match("/nextval\\('\"?\\w+\"?\.?\"?\\w+\"?'(::regclass)?\\)/", $column->defaultValue) === 1) {
				$table->sequenceName = preg_replace(['/nextval/', '/::/', '/regclass/', '/\'\)/', '/\(\'/'], '', $column->defaultValue);
			}
			$column->defaultValue = null;
		} elseif ($column->defaultValue) {
			if ($column->type === 'timestamp' && $column->defaultValue === 'now()') {
				$column->defaultValue = new Expression($column->defaultValue);
			} elseif ($column->type === 'boolean') {
				$column->defaultValue = ($column->defaultValue === 'true');
			} elseif (strncasecmp($column->dbType, 'bit', 3) === 0 || strncasecmp($column->dbType, 'varbit', 6) === 0) {
				$column->defaultValue = bindec(trim($column->defaultValue, 'B\''));
			} elseif (preg_match("/^'(.*?)'::/", $column->defaultValue, $matches)) {
				$column->defaultValue = $column->phpTypecast($matches[1]);
			} elseif (preg_match('/^(\()?(.*?)(?(1)\))(?:::.+)?$/', $column->defaultValue, $matches)) {
				if ($matches[2] === 'NULL') {
					$column->defaultValue = null;
				} else {
					$column->defaultValue = $column->phpTypecast($matches[2]);
				}
			} else {
				$column->defaultValue = $column->phpTypecast($column->defaultValue);
			}
		}
	}

	return true;
}
相关推荐
前端世界17 小时前
Python 正则表达式实战:用 Match 对象轻松解析拼接数据流
python·正则表达式·php
苏琢玉20 小时前
用 PHP 玩向量数据库:一个从小说网站开始的小尝试
php·composer
wuk99821 小时前
ThinkPHP 6框架常见错误:htmlentities()函数参数类型问题解决
php
万岳软件开发小城1 天前
开源与定制化对比:哪种在线教育系统源码更适合教育培训APP开发?
开源·php·软件开发·在线教育系统源码·教育小程序·教育app开发
lskblog1 天前
Composer安装教程及国内镜像设置(含腾讯云、阿里云镜像)
阿里云·php·腾讯云·laravel·composer
m0_738120721 天前
CTFshow系列——PHP特性Web93-96
开发语言·安全·web安全·php·ctfshow
@CLoudbays_Martin111 天前
为什么动态视频业务内容不可以被CDN静态缓存?
java·运维·服务器·javascript·网络·python·php
learning_tom2 天前
HTML图片标签及路径详解
linux·服务器·php
魔道不误砍柴功2 天前
Mac 能够连Wife,但是不能上网问题解决
网络·macos·php
搬码临时工2 天前
怎样让外网计算机访问局域网计算机?通过公网地址访问不同内网服务的设置方法
开发语言·php