CI update_batch; Concatenate and graft variable and string as array value

Using CI update_batch()

I am unable to set the value where I increment the integer db value. This works well with CI set(); update()

, but the package is the best option as there are several updates.

I have columns that all have the same characters at the end of the name, with different characters at the beginning (different years): 2014x, 2015x, 2016x, etc. So I created $var

one that identifies the year, then add the string 'x' and 'y'

via concatenation. Finally, the parameter value in the array must increase by 1, so I add +1. This concatenation works great in keys - that is, I'm updating the correct column and fields.

$data = array(
    array('name' => $name1,
            $var.'x' => $var.'x+1'),
    array('name' => $name2,
            $var.'y' => $var.'y+1')
            );          
$this->db->update_batch('my_table', $data, 'tname');

      

In the above case, the fields are updated with only the value of the $var

year that was defined.

I've also tried the following:

=> '{"$var.x+1"}'  // places a '0' value in the field
=> $var.'x' +1       // places the value of $var
=> '$var.x+1'        // places a '0' value in the field

      

How can I use update_batch()

to increase my margin by 1

?

As an example, this code works successfully:

$this->db->where('name',$name1);
$this->db->set($var.'x',$var.'x+1',FALSE);
$this->db->update('my_table');

      

+3


source to share


1 answer


You cannot do this via update_batch.

The document https://ellislab.com/codeigniter/user-guide/database/active_record.html says it like this:

Note. All values ​​are automatically escaped, creating safer queries.



how to simply repeat db-> set? eg.

$this->db->where('name',$name1);
for($var = 2004; $var<2008;$var++) {
   $this->db->set($var.'x',$var.'x+1',FALSE);
   $this->db->set($var.'y',$var.'y+1',FALSE);
}
$this->db->update('my_table');

      

+2


source







All Articles