【问题标题】:Codeigniter update_batch increment valueCodeigniter update_batch 增量值
【发布时间】:2017-04-06 09:11:56
【问题描述】:

如果我错了,请纠正我,我正在尝试更新多行,增加它的值,据我所知,使用 update_batch() 是不可能的,因为 CI 总是在使用 update_batch() 时转义值,这是否正确,或者如果有办法,请详细说明该方式作为答案。例如,我有这个数组:

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

现在我想通过将列 value(INT) 增加数组中的值来更新我的表,其中列 product_id = product_id 在数组中。

我现在正在做的是使用这样的集合循环

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

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

非常欢迎和赞赏所有答案。 提前谢谢你。

【问题讨论】:

    标签: php mysql arrays codeigniter


    【解决方案1】:

    您可以使用$this->db->update_batch(); 一次更新多条记录。请参阅此 codeigniter user guide 进行更新查询。

    【讨论】:

    • 感谢您的回答,是的,但是如何增加值?即:如果在我的值列中我的值已经是 50,如果我使用 update_batch (value=>'5'),我的表中的值将变为 5,而我想要的是变为 55
    • 你需要从数据库中获取旧值并添加新值,并创建关联数组以批量更新。
    • 与在所有阵列上使用 foreach(set and update) 相比,这不会更昂贵和更重吗?
    • 我不这么认为。无论如何,您在$array 中获得了旧值。一个一个更新,最好一次更新。
    【解决方案2】:

    IN CODEIGNITER 3/4 对于 self 字段的更新批量算术运算没有解决方案,因此您需要编辑系统文件,因为它可能有风险但可能对您有用,在路径系统中打开名为 db_query_builder.php 的文件\ database\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)
    {
        /* The $val[$field]['value'][0] 
           and $val[$field]['value'][1] are unidentical type so we are 
           converting it to string by removing the single quote and using 
           settype for string type.
        */
        $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'];
    

    现在在为 update_batch 定义数组时,只需使用算术运算符和数量传递值数组。

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

    在上面的代码中,您正在识别值数组并使用算术符号进行验证并执行自身字段计算。

    Laravel 也不支持更新批量算术运算,但有 github repo:https://github.com/mavinoo/laravelBatch#example-increment--decrement

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-09
    • 1970-01-01
    • 2012-05-11
    • 2019-01-20
    • 2019-04-05
    相关资源
    最近更新 更多