[英]Updating multiple records in database using Codeigniter
Am trying to update multiple records in the database but my case I have a column total, which I want to update to different values. 我正在尝试更新数据库中的多个记录,但是我的情况下我有一个总计列,我想将其更新为不同的值。 I haven't tried much here but hope I could get a clue on how to go about this.
我在这里没有做太多的尝试,但是希望我能从中获得线索。
My controller 我的控制器
public function update_record()
{
$id = ["19821", "19923", "19966", "19967"];
$total = ["8", "118", "90", "100"];
if ($this->some_model->batch_data('records', $total, $id) == true) {
echo "yes";
} else {
echo "no";
}
}
The Model 该模型
public function batch_data($table, $data, $where)
{
$this->db->where_in('id',$where);
$this->db->set('total',$data);
$this->db->update($table);
return true;
}
I have not tested this yet but currently looking for a more and efficient way of doing this. 我尚未对此进行测试,但目前正在寻找一种更有效的方法。
Try this, Only for single where condition
试试这个,仅针对
single where condition
Controller: 控制器:
public function update_record()
{
$tableName = "records";
$id = ["19821", "19923", "19966", "19967"];
$total = ["8", "118", "90", "100"];
$update_data = [];
foreach ($id as $key => $value) {
$update_data[] = [
'id' => $value,
'total' => $total[$key]
];
}
$whereKey = 'id';
$this->$this->some_model->batch_data($tableName, $updateData, $whereKey);
}
Model: 模型:
public function batch_data($tableName, $updateData, $whereKey)
{
$this->db->update_batch($tableName, $updateData, $whereKey);
}
As per my comment. 根据我的评论。 This is a method that combines the array in to key/value pairs and updates them 1x1 wrapped in a transaction (so if one query fails nothing changes).
这种方法将数组组合为键/值对,并以事务处理的1x1更新它们(因此,如果一个查询失败,则不会有任何变化)。
This is the method I would personally use as I don't like update_batch
s internal workings (cases). 这是我个人使用的方法,因为我不喜欢
update_batch
的内部工作原理(案例)。
$id = ["19821", "19923", "19966", "19967"];
$total = ["8", "118", "90", "100"];
$combined = array_combine($id, $total);
if (count($combined) > 0) {
$this->db->trans_start();
foreach ($combined as $id => $total) {
$this->db->set('total', $total);
$this->db->where('id', $id);
$this->db->update('sometable');
}
$this->db->trans_complete();
return $this->db->trans_status();
}
return true;
If you still want to update multiple records using the update_batch
method, you could first assign the id
and total
as key-value arrays, then use the update_batch
method. 如果仍然要使用
update_batch
方法更新多个记录,则可以先将id
和total
分配为键值数组,然后使用update_batch
方法。
Controller : 控制器 :
public function update_record()
{
$id = ["19821", "19923", "19966", "19967"];
$total = ["8", "118", "90", "100"];
$update_data = [];
foreach ($id as $key => $value) {
$update_data[] = [
'id' => $value,
'total' => $total[$key]
];
}
if ($this->some_model->batch_data('records', $update_data) == true) {
echo "yes";
} else {
echo "no";
}
}
Model : 型号 :
public function batch_data($table, $data)
{
$this->db->update_batch($table, $data, 'id'); // this will set the id column as the condition field
return true;
}
Output : 输出 :
// preview query output :
// UPDATE `records`
// SET
// `total` =
// CASE
// WHEN `id` = '19821' THEN 8
// WHEN `id` = '19923' THEN 118
// WHEN `id` = '19966' THEN 90
// WHEN `id` = '19967' THEN 100
// ELSE `total`
// END
// WHERE `id` IN ('19821', '19923', '19966', '19967')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.