由於項目需要使用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;
}
}