簡體   English   中英

我可以將數組添加到 where 子句到 codeigniter 中的 update_batch

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

我正在使用 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')); 

如此多的條件添加。

這可能嗎?

例如,您可以通過在模型 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);
}

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 參數是一個靜態值。

您可以使用以下代碼更新您的數據:

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

你總是可以這樣做:

$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這缺少update_batch()所需的where參數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM