简体   繁体   中英

Codeigniter update_batch increment value

Please correct me if I'm wrong, I am trying to update multiple rows, incrementing its value, from what I know, it is impossible to do this using update_batch() as CI always escape the value when using update_batch(), is this correct, or if there is a way, please elaborate the way as an answer. For example, I have this array:

$array = array(
    array(
        'product_id' => '1',
        'value' => '5',
    ),
    array(
        'product_id' => '2',
        'value' => '15'
    )
);

Now I want to update my table by incrementing column value(INT) by the value in the array where column product_id = product_id in the array.

What I'm doing now is using loops of set like this

foreach($array as $a) {
    $this->db->set('value' + $a['value'], FALSE);
    $this->db->where('product_id', $a['product_id'];
    $this->db->update('table');
}

However sometimes I will need to update more than 10 rows, and I'm thinking this process will be costly and take a lot of memory, how do i further simplify this method to get the same result ?

All answers are greatly welcome and appreciated. Thank you in advance.

You can use $this->db->update_batch(); to update multiple record at once. See this codeigniter user guide for update query.

FOR LARAVEL THERE IS SOLUTION : https://github.com/mavinoo/laravelBatch#example-increment--decrement

FOR CODEIGNITER 3/4 : line number 1970 in db_query_builder.php.

//if field name in value and +,-,*,/,% in value then apply that to self field
$flag = TRUE;
$then = 'THEN ';
if(is_array($val[$field]['value']) && count($val[$field]['value']) == 2)
{
    $first = str_replace("'", "", $val[$field]['value'][0]);
    $second = str_replace("'", "", $val[$field]['value'][1]);
    settype($first, "string");
    settype($second, "string");
    switch ($first) {
        case "+":
            $then .= $val[$field]['field'].' + ';
            break;
        case '-':
            $then .= $val[$field]['field'].' - ';
            break;
        case "*":
            $then .= $val[$field]['field'].' * ';
            break;
        case '/':
            $then .= $val[$field]['field'].' / ';
            break;
        case '%':
            $then .= $val[$field]['field'].' % ';
            break;
        
        default:
            $flag = FALSE;
            break;
    }

    if(is_numeric($second) && $flag == TRUE)
    {
        $then .= $val[$field]['value'][1];
    }

}
else
{
    $then = 'THEN '.$val[$field]['value'];
}
$final[$val[$field]['field']][] = 'WHEN '.$val[$index]['field'].' = '.$val[$index]['value'].' '.$then;

//$final[$val[$field]['field']][] = 'WHEN '.$val[$index]['field'].' = '.$val[$index]['value'].' THEN '.$val[$field]['value'];


$values[] = array('id','balance' => ['+',500]); //will increment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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