7

I am using codeigniter update_batch function.

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

j0k
  • 22,600
  • 28
  • 79
  • 90
Kanishka Panamaldeniya
  • 17,302
  • 31
  • 123
  • 193
  • 2
    I think your best bet would be to overwrite the update_batch function with your own. This functionality doesn't exist in the function. – Brendan Mar 05 '12 at 16:15
  • 2
    I hope the CodeIgniter team will create a multibatch insert/update method for the framework. For me the update batch method for 1 index is 1600% faster then updating the database line by line. – machineaddict Apr 10 '13 at 13:19

4 Answers4

1

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:

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);
}
ermacmkx
  • 439
  • 5
  • 12
1

You can use below code to update your data:

$this->db->update("TableName",[dataarray],[where condition array]);
Shawn
  • 1,232
  • 1
  • 14
  • 44
Devang Hire
  • 174
  • 1
  • 5
0

This update_batch function does not allow WHERE parameter, but you can try splitting it before calling the function.

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.

Rodrigo Prazim
  • 788
  • 7
  • 14
-3

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().

tim peterson
  • 23,653
  • 59
  • 177
  • 299
WittyPleb
  • 553
  • 2
  • 10
  • 22