Codeigniter和Oracle获取插入查询的last_id

时间:2017-09-10 03:07:49

标签: php mysql oracle codeigniter insert-id

我有这样的模特:

 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?)

1 个答案:

答案 0 :(得分:0)

在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;

希望它对你有所帮助;快乐的编码

相关问题