本文主要是介绍Yii2 ActiveRecord连接OpenGauss提示表不存在table not exist,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.修改数据库连接信息
文件位置 config/db.php
添加默认Schema
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)
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;
}
这篇关于Yii2 ActiveRecord连接OpenGauss提示表不存在table not exist的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!