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;
}
相关推荐
保持低旋律节奏7 小时前
linux——进程状态
android·linux·php
计算机学姐7 小时前
基于php的摄影网站系统
开发语言·vue.js·后端·mysql·php·phpstorm
计算机学姐8 小时前
基于php的旅游景点预约门票管理系统
开发语言·后端·mysql·php·phpstorm
百***07458 小时前
GPT-5.2国内稳定接入实战指南:中转调用全链路方案(Python适配)
python·gpt·php
小阿宁的猫猫10 小时前
sqlmap的使用
sql·网络安全·php
roman_日积跬步-终至千里11 小时前
【Starrocks】StarRocks 排错:`Invalid method name: ‘heartbeat‘`(BE 心跳端口/协议错误)
服务器·网络·php
Maybe I Simple11 小时前
PHP常用方法封装
php
slongzhang_11 小时前
PHP图片处理|画布入门
开发语言·php
知码者12 小时前
对于Thinkphp5可能遇到的保存问题
服务器·php·apache·小程序开发·跨平台小程序
郑州光合科技余经理12 小时前
实战分享:如何构建东南亚高并发跑腿配送系统
java·开发语言·javascript·spring cloud·uni-app·c#·php