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;
}
相关推荐
鱼嘻2 小时前
线程邮箱框架与示例
linux·c语言·开发语言·算法·php
zh73143 小时前
支付宝沙盒模式商家转账经常出现 响应异常: 解包错误
前端·阿里云·php
Q_Q19632884753 小时前
python小区物业管理系统-小区物业报修系统
开发语言·spring boot·python·django·flask·node.js·php
泪不是Web妳而流7 小时前
【CTFSHOW_Web入门】命令执行
web安全·网络安全·php·linux命令·rce·命令执行·ctfshow命令执行wp题解
菜鸟、小高8 小时前
Yii2.0 模型规则(rules)详解
php·yii
深山技术宅17 小时前
在Laravel 12中实现基于parent_id的树状数组
php·laravel
海尔辛1 天前
学习黑客 MAC 地址深入了解
学习·macos·php
Q_Q19632884751 天前
python小说网站管理系统-小说阅读系统
开发语言·spring boot·python·django·flask·node.js·php
2501_906314321 天前
使用Scrapeless Scraping Browser的自动化和网页抓取最佳实践
搜索引擎·自动化·php
pqq的迷弟1 天前
redis多路复用IO模型 以及 6.0引入的多线程模型
数据库·redis·php