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;
}
相关推荐
痴人说梦梦中人6 小时前
自建 dnslog 回显平台:渗透测试场景下的隐蔽回显利器
web安全·网络安全·渗透测试·php·工具
我是老孙12 小时前
windows10 php报错
开发语言·php
小红帽2.016 小时前
开源PHP在线客服系统源码搭建教程
开发语言·开源·php
CRMEB定制开发17 小时前
CRMEB 框架中 PHP 上传扩展开发:涵盖本地上传及阿里云 OSS、腾讯云 COS、七牛云
阿里云·php·腾讯云·微信商城·php商城源码
hao_wujing18 小时前
攻击模型的恶意行为检测
网络·数据库·php
Yungoal1 天前
php & apache构建 Web 服务器
服务器·php·apache
浩浩测试一下1 天前
Authpf(OpenBSD)认证防火墙到ssh连接到SSH端口转发技术栈 与渗透网络安全的关联 (RED Team Technique )
网络·网络协议·tcp/ip·安全·网络安全·php
孤寂大仙v1 天前
【计算机网络】网络层IP协议与子网划分详解:从主机通信到网络设计的底层逻辑
tcp/ip·计算机网络·php
vortex52 天前
浅谈 Linux 防火墙:从原理到实践
linux·网络·php
come112342 天前
Claude 写 PHP 项目的完整小白教程
开发语言·php