我可以帮您翻译成中文:在 CodeIgniter 中的 update_batch 函数中,我能否添加数组到 where 子句中?

7

我正在使用CodeIgniter的update_batch函数。

我想将一个数组作为第三个参数(where子句)传递给update_batch

$data = array(
   array(
      'title' => 'My title' ,
      'name' => 'My Name 2' ,
      'date' => 'My date 2'
   ),
   array(
      'title' => 'Another title' ,
      'name' => 'Another Name 2' ,
      'date' => 'Another date 2'
   )
);

不要写成这样:

$this->db->update_batch('mytable', $data, 'title'); 

我想要做这件事情:
 $this->db->update_batch('mytable', $data, array('title','name')); 

所以要添加多个where条件。

这是否可能?


2
我认为你最好的选择是用自己的函数覆盖update_batch函数。这个功能在原有函数中并不存在。 - Brendan
2
我希望CodeIgniter团队能够为该框架创建一个多批量插入/更新方法。对我而言,针对1个索引的更新批处理方法比逐行更新数据库快1600%。 - machineaddict
4个回答

1
你可以通过在你的模型 Test_model 中创建批量更新的方法来实现此操作,例如,因为 Codeigniter 不支持原生的 update_batch 中的多个 where 条件,所以如下所示的示例:
public function update_batch_custom($table_name, $data, $indexes) {
    if (empty($data) || empty($indexes)){
        return 'Data or indexes must not be empty';
    }

    $db = $this->load->database('test_db', TRUE);
    $sql = 'UPDATE ' . $table_name . ' SET ' . "\n";

    //columns on which is done actual update
    $columns = [];
    foreach ($data[0] as $key => $value) {
        if (!in_array($key, $indexes)){
            $columns[] = $key;
        }
    }

    /*
     * forming WHEN .. THEN sql parts and WHERE condition
     */
    $parts = [];
    $where = [];
    foreach ($data as $row) {
        foreach ($columns as $column) {
            $sql_part = ' WHEN (';
            foreach ($indexes as $index) {
                 $sql_part .=  $index . '= \''.$row[$index] . '\' AND ';
                 $where[$index][] = $row[$index];
            }

            $sql_part = substr($sql_part, 0, -4);
            $sql_part .= ') THEN \'' . $row[$column] . '\'';
            $parts[$column][] = $sql_part;
        }
    }

    /*
     * connecting WHEN .. THEN parts for each column to be updated
     */
    foreach ($columns as $column) {
        $sql .= $column .'= CASE ';
        foreach ($parts[$column] as  $sql_part) {
            $sql .= $sql_part;
        }
        $sql .= ' ELSE ' . $column . ' END,';
    }

    /*
     * adding WHERE part
     */
    $sql = substr($sql, 0, -1);
    $sql .= ' WHERE ';
    foreach ($indexes as $index) {
        if ( count($where[$index]) > 0){
            $unique_where = array_unique($where[$index]);
            $sql .= $index . ' IN (\'' . join('\',\'', $unique_where) . '\') AND ';
        }
    }

    $sql = substr($sql, 0, -4);
    $sql .= ';';

    return $db->query($sql);
}

1
您可以使用以下代码来更新您的数据:

$this->db->update("TableName",[dataarray],[where condition array]);

0

这个 update_batch 函数不允许使用 WHERE 参数,但是你可以在调用函数之前尝试将其拆分。

试试这个:

$data = array(
    array(
        'title' => 'My title' ,
        'name' => 'My Name 2' ,
        'date' => 'My date 2'
    ),
    array(
        'title' => 'Another title' ,
        'name' => 'Another Name 2' ,
        'date' => 'Another date 2'
    )
);

$this->db->where('name','My Name 2');
$this->db->update_batch('mytable', $data, 'title');

控制器:

$chunk1 = array_chunk($data,100);
for($i=0;$i < count($chunk1);$i++) {
   $this->upload_model->update_data($chunk1[$i],'My Name 2');
}

模型:

public function update_data($data='',$name=''){
   $this->db->where('name',$name);
   $this->db->update_batch('mytable', $data, 'title');
}

请记住,我的WHERE参数是一个静态值。


-3
你可以这样做:
$data = array(
   array(
      'title' => 'My title' ,
      'name' => 'My Name 2' ,
      'date' => 'My date 2'
   ),
   array(
      'title' => 'Another title' ,
      'name' => 'Another Name 2' ,
      'date' => 'Another date 2'
   )
);
$this->db->where(array('title' => 'title', 'name' => 'name'));
$this->db->update_batch('mytable', $data);

未经测试。

更新:在update_batch()中缺少必需的where参数。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接