由于项目需要使用Oracle数据库,之前遇到一个insert不成功。原以为官方会解决,但是到目前应该还是没解决。
以下是我自己的解决办法,这样修改不清楚有什么不良效果,但能实现功能(MYSQL不合适)
thinkphp\library\think\db\Query.php 中的insert
public function insert(array $data = [], $replace = false, $getLastInsID = false, $sequence = null)
{
// 分析查询表达式
$options = $this->parseExpress();
$data = array_merge($options['data'], $data);
// 生成SQL语句
$sql = $this->builder->insert($data, $options, $replace);
// 获取参数绑定
$bind = $this->getBind();
if ($options['fetch_sql']) {
// 获取实际执行的SQL语句
return $this->connection->getRealSql($sql, $bind);
}
// 执行操作
//$result = 0 === $sql ? 0 : $this->execute($sql, $bind);
//修改
$realsql=$this->connection->getRealSql($sql, $bind);
$result = $this->execute($realsql);
//修改
if ($result) {
$sequence = $sequence ?: (isset($options['sequence']) ? $options['sequence'] : null);
$lastInsId = $this->getLastInsID($sequence);
if ($lastInsId) {
$pk = $this->getPk($options);
if (is_string($pk)) {
$data[$pk] = $lastInsId;
}
}
$options['data'] = $data;
$this->trigger('after_insert', $options);
if ($getLastInsID) {
return $lastInsId;
}
}
return $result;
}
复制代码
thinkphp\library\think\db\Connection.php中的getLastInsID
public function getLastInsID($sequence = null)
{
// return $this->linkID->lastInsertId($sequence);
switch($this->config['type']) {
case '\think\oracle\Connection':
if(preg_match("/^\s*(INSERT\s+INTO)\s+(\w+)\s+/i", $this->queryStr, $match)) {
$seq=$this->config['sequence_prefix'];
$fix1=$this->config['prefix'];
$fix=str_ireplace($fix1, "", $match[2]);
$this->table = $seq.$fix;
}
$sequence = $this->table;
$vo = $this->query("SELECT {$sequence}.currval currval FROM dual");
return $vo?$vo[0]["currval"]:0;
}
}