簡體   English   中英

Codeigniter和Oracle獲得插入查詢的last_id

[英]Codeigniter and Oracle get the last_id of insert query

我有這樣的模型:

 function data_input($data, $file_upload) {
    $this->db->set('ID_KEY', "LASTID_SEQ.NEXTVAL", FALSE); //false escape
    $this->db->insert('FIRST_TABLE', $data);
    $this->db->set('ID_KEY', "LASTID_SEQ.NEXTVAL -1", FALSE); //false escape
    $this->db->insert('SECOND_TABLE', $file_upload);
    return true;
}

我想將ID_KEY的值發送到控制器,並使用它基於ID_KEY更新數據庫。

我的問題是,我可以生成ID_KEY值,該值在FIRST_TABLE和SECOND_TABLE中是相同的,但是我無法將該值發送到控制器。

或者,我可以使用另一種方法來獲取oracle活動記錄中insert的"insert_id()"的值。 (或使用$this->db->query嗎?)

在Codeigniter中; 調用$ this-> db-> insert_id()返回最后插入的數據的ID。 如果您打算返回兩個查詢的最后一個插入ID,我會這樣做:

function data_input($data, $file_upload) {
$insert_id_collection = array();

// first table query
$this->db->set('ID_KEY', "LASTID_SEQ.NEXTVAL", FALSE); //false escape
$this->db->insert('FIRST_TABLE', $data);
$insert_id_collection['first_table_name'] = $this->db->insert_id();

// second table query
$this->db->set('ID_KEY', "LASTID_SEQ.NEXTVAL -1", FALSE); //false escape
$this->db->insert('SECOND_TABLE', $file_upload);
$insert_id_collection['second_table_name'] = $this->db->insert_id();

// this will contain to last insert ID;
return $insert_id_collection;

}

我之所以將數組索引到表名以供您標識最后一個插入ID是因為:

希望對您有幫助; 快樂編碼

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM