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 小时前
解决PHP未检测到您服务器环境的sqlite3数据库扩展报错
服务器·数据库·php
名字不相符19 小时前
攻防世界WEB难度一(个人记录)
学习·php·web·萌新
Q_Q51100828520 小时前
python+django/flask的结合人脸识别和实名认证的校园论坛系统
spring boot·python·django·flask·node.js·php
Q_Q51100828520 小时前
python+django/flask的选课系统与课程评价整合系统
spring boot·python·django·flask·node.js·php
权泽谦20 小时前
PHP 版羊了个羊完整开发实战:逻辑解析 + 三消算法 + 全套接口(附源码)
开发语言·php
数据牧羊人的成长笔记21 小时前
Hadoop 分布式计算MapReduce和资源管理Yarn 2
开发语言·php
JaguarJack1 天前
PHP True Async RFC 被拒——原生异步离 PHP 还有多远?
php·服务端
JSON_L1 天前
Fastadmin 使用RabbitMQ队列
rabbitmq·php·fastadmin
Gerardisite1 天前
如何在微信个人号开发中有效管理API接口?
java·开发语言·python·微信·php
last demo2 天前
MariaDB 数据库管理
linux·运维·服务器·数据库·php·mariadb