【发布时间】:2013-12-17 02:22:08
【问题描述】:
我可以在 codeigniter 中使用$this->db->insert_id(); 获取最后插入的 id,有什么方法可以获取最后更新记录的 id?我用相同的方法尝试过,即$this->db->insert_id();,但它不起作用(改为返回 0)。
【问题讨论】:
标签: php codeigniter activerecord codeigniter-2
我可以在 codeigniter 中使用$this->db->insert_id(); 获取最后插入的 id,有什么方法可以获取最后更新记录的 id?我用相同的方法尝试过,即$this->db->insert_id();,但它不起作用(改为返回 0)。
【问题讨论】:
标签: php codeigniter activerecord codeigniter-2
Codeigniter 不支持。我必须这样做:
$updated_id = 0;
// get the record that you want to update
$this->db->where(array('vrnoa'=>$data['vrnoa'], 'etype' => 'sale'));
$query = $this->db->get('StockMain');
// getting the Id
$result = $query->result_array();
$updated_id = $result[0]['stid'];
// updating the record
$this->db->where(array('vrnoa'=>$data['vrnoa'], 'etype' => 'sale'));
$this->db->update('StockMain',$data);
【讨论】:
$this->db->insert_id();
这只会给出插入的 id。要获取更新的行 ID,您可以添加一个列作为 lastmodified(时间戳),并在每次运行更新查询时使用当前时间戳更新此列。在你的更新查询之后运行这个:
$query = $this->db->query('SELECT id FROM StockMain ORDER BY lastmodified DESC LIMIT 1');
$result = $query->result_array();
你会得到结果集中的id。
【讨论】:
这是你可以做到最短的方法
$where = array('vrnoa'=>$data['vrnoa'], 'etype' => 'sale');
//更新记录
$this->db->where($where);
$this->db->update('StockMain',$data);
//获取记录
$this->db->where($where);
return $this->db->get('StockMain')->row()->stid;
【讨论】:
返回您在 where 子句中使用的 id 进行更新
function Update($data,$id){
$this->db->where('id', $id);
$this->db->update('update_tbl',$data);
return $id;
}
【讨论】:
使用 codeigniter 和 MY_MODEL 扩展版本。这是我获得 relfe 的瓶颈之一。
function update_by($where = array(),$data=array())
{
$this->db->where($where);
$query = $this->db->update($this->_table,$data);
return $this->db->get($this->_table)->row()->id; //id must be exactly the name of your table primary key
}
调用此 Updates 并获取更新后的 id。我猜两次运行查询有点矫枉过正,但以上所有内容也是如此。
你怎么称呼?
$where = array('ABC_id'=>5,'DEF_ID'=>6);
$data = array('status'=>'ACCEPT','seen_status' =>'SEEN');
$updated_id= $this->friends->update_by($where,$data);
【讨论】:
试试这样:
//update
public function update($table, $where, $data)
{
// get the record that you want to update
$this->db->where($where);
$query = $this->db->get($table);
// getting the Id
$row = array_values($query->row_array());
$updated_id = $row[0];
// updating the record
$updated_status = $this->db->update($table, $data, $where);
if($updated_status):
return $updated_id;
else:
return false;
endif;
}
【讨论】: