简体   繁体   English

我可以将数组添加到 where 子句到 codeigniter 中的 update_batch

[英]can I add array to where clause to update_batch in codeigniter

I am using codeigniter update_batch function.我正在使用 codeigniter update_batch函数。

I want to pass an array as the third parameter (where clause) to 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'
   )
);

Instead of this:取而代之的是:

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

I want to do this:我想做这个:

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

So multiple where conditions added.如此多的条件添加。

Is this possible?这可能吗?

You could do this by creating your method for batch update in your model Test_model for example because Codeigniter does not supports multiple where condition in native update_batch, so example below:例如,您可以通过在模型 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);
}

This update_batch function does not allow WHERE parameter, but you can try splitting it before calling the function.update_batch函数不允许WHERE参数,但您可以在调用该函数之前尝试拆分它。

Try this:尝试这个:

$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');

Controller:控制器:

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

Model:模型:

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

Remember, my WHERE parameter is a static value.请记住,我的 WHERE 参数是一个静态值。

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

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

You could always do this:你总是可以这样做:

$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);

Not tested.未测试。

UPDATE This lacks the required where parameter in update_batch() . UPDATE这缺少update_batch()所需的where参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM