简体   繁体   English

Codeigniter update_batch 增量值

[英]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.如果我错了,请纠正我,我正在尝试更新多行,增加其值,据我所知,使用 update_batch() 不可能做到这一点,因为 CI 在使用 update_batch() 时总是会转义值,是这样吗正确,或者如果有办法,请详细说明方式作为答案。 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.现在我想通过将列值(INT)增加数组中的值来更新我的表,其中列 product_id = product_id 在数组中。

What I'm doing now is using loops of set like this我现在正在做的是使用这样的 set 循环

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 ?但是有时我需要更新超过 10 行,我认为这个过程会很昂贵并且会占用大量内存,我如何进一步简化这个方法以获得相同的结果?

All answers are greatly welcome and appreciated.非常欢迎和感谢所有答案。 Thank you in advance.先感谢您。

You can use $this->db->update_batch(); 您可以使用$this->db->update_batch(); to update multiple record at once. 一次更新多个记录。 See this codeigniter user guide for update query. 请参阅此codeigniter用户指南以进行更新查询。

FOR LARAVEL THERE IS SOLUTION : https://github.com/mavinoo/laravelBatch#example-increment--decrement对于 LARAVEL 有解决方案: https : //github.com/mavinoo/laravelBatch#example-increment--decrement

FOR CODEIGNITER 3/4 : line number 1970 in db_query_builder.php. FOR CODEIGNITER 3/4:db_query_builder.php 中的第 1970 行。

//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.

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

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