【问题标题】:Codeigniter and Oracle get the last_id of insert queryCodeigniter 和 Oracle 获取插入查询的 last_id
【发布时间】:2018-02-18 14:05:30
【问题描述】:

我有这样的模型:

 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_id()"的值。 (或使用$this->db->query?)

【问题讨论】:

    标签: php mysql oracle codeigniter insert-id


    【解决方案1】:

    在 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是那个;

    希望对您有所帮助;快乐编码

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-23
      • 1970-01-01
      • 1970-01-01
      • 2020-09-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多