SQLSTATE[HY000]: General error: 936 OCIStmtExecute: ORA-00936: 缺失表达式 (ext\pdo_oci\oci_statement.c:157)
/**
* 获取最近插入的ID
* @access public
* @param BaseQuery $query 查询对象
* @param string $sequence 自增序列名
* @return mixed
*/
public function getLastInsID(BaseQuery $query, string $sequence = null)
{
$pdo = $this->linkID->query("select {$sequence}.currval as id from dual");
$result = $pdo->fetchColumn();
return $result;
}
protected function supportSavepoint(): bool
{
return true;
}
错误现象:
使用thinkphp6连接oracle数据库时
使用save方法永远跳转到update
使用insert方法执行语句成功,但是总是报ORA-00936错误
原因分析:
tp6主要是基于mysql
进行设计,mysql强制要求每个表都有主键,然而,oracle对此没有硬性要求。
所以tp6在insert或者update访问数据表时候都会先取得主键,然后再进行访问。
由于oracle大部分表格都没有主键,所以抛出错误。
解决方法:
临时方法:禁止tp6在insert和update时获得主键。
具体操作:
\vendor\topthink\think-orm\src\db\PDOConnection.php
944行~945行,注释掉判断函数,然后让$lastInsId强制为0
$sequence = $options['sequence'] ?? null;
//$lastInsId = $this->getLastInsID($query, $sequence);
$lastInsId = 0;
controller当中使用函数:
(xxxxx是表名)
Db::table('xxxxx')->update($data);
Db::table('xxxxx')->insert($data);
xxxxx::update($data);
xxxxx::insert($data);
注意,这个方法不能解决save总是跳转到update的方法,所以老老实实用update和insert吧。