1、简单说明
- 旧的项目使用的 ThinkPHP 5.0 ;
- Model配置PostgreSql会出现各种报错;
model一般配置
class Demo extends Model { protected $connection = 'pgsql'; protected $table = 'xxx.xxx'; }
2、兼容性修改
1)在pgsql库上运行sql文件(thinkphp/library/think/db/connector/pgsql.sql)
主要为了创建几个自定义函数( "public"."table_msg"),框架运行需要用到;
对于 PostgreSql11 以上版本会报错,某些字段已经废弃掉了,需要如下修改:
找到这行: pg_attrdef.adsrc AS fields_default
改成:pg_get_expr(pg_attrdef.adbin, pg_attrdef.adrelid) AS fields_default
运行成功后会有两个重载函数(第一个默认schema=public):
- "public"."table_msg" (a_table_name varchar)
- "public"."table_msg"(a_schema_name varchar, a_table_name varchar)
2)修改connector文件(thinkphp/library/think/db/connector/Pgsql.php)
主要针对方法:getFields($tableName) 需要用到上面创建的函数进行获取字段类型等;
找到这2行,可以看出,无论怎么配置表名,都会使用上门第一个重载方法(默认schema=public)
list($tableName) = explode(' ', $tableName);
$sql = 'select fields_name as "field",fields_type as "type",fields_not_null as "null",fields_key_name as "key",fields_default as "default",fields_default as "extra" from table_msg(\'' . $tableName . '\');';
需要进行兼容改造,支持配置:xxx.xxx 的表名(schema_name+table_name)
list($schemeName, $tableName) = explode('.', $tableName);
$sql = 'select fields_name as "field",fields_type as "type",fields_not_null as "null",fields_key_name as "key",fields_default as "default",fields_default as "extra" from table_msg(\'' . $schemeName . '\',\'' . $tableName . '\');';
3) 兼容 pg connector 的 getLastInsID 方法
由于pg表的不一定都有自增字段,当调用 $mod->insert() /save() 等方法就会异常退出(因为该表没有sequence 类型字段)
源码:
源码:$this->connection->getLastInsID($sequence)
所以需要在此文件(thinkphp/library/think/db/connector/Pgsql.php)重写方法 getLastInsID(),增加try-catch 处理即可
try { return parent::getLastInsID($sequence); } catch (\Exception $e) { return null; }
- PostgreSql thinkphp5 thinkphp modelpostgresql thinkphp5 thinkphp model thinkphp5 thinkphp5 weblogic thinkphp thinkphp5框架thinkphp数据库 thinkphp5 thinkphp接口 数据 漏洞分析 漏洞thinkphp5 thinkphp thinkphp thinkphp3 thinkphp5常用 thinkphp5 thinkphp sdk htmlentities thinkphp5 parameter thinkphp thinkphp5 thinkphp paginate日期