drmy1050 2017-01-27 07:50
浏览 223
已采纳

使用update_batch增加数据库中的字段自动添加单引号

Using update_batch to increment fields in database Single quotes automatically added calling function code

$this->db->update_batch("FundCategory",$create_data,'fundCategoryId')

My codigniter last query is

UPDATE `FundCategory` SET `fundCategoryUpdatedById` = CASE 
WHEN `fundCategoryId` = '4' THEN '20000045'
WHEN `fundCategoryId` = '4' THEN '20000045'
ELSE `fundCategoryUpdatedById` END, `fundCategoryValue` = CASE 
WHEN `fundCategoryId` = '4' THEN 'fundCategoryValue + 100'
WHEN `fundCategoryId` = '4' THEN 'fundCategoryValue + 200'
ELSE `fundCategoryValue` END
WHERE `fundCategoryId` IN('4','4')

I want to remove single quotes after then when increment "fundCategoryValue" column

  • 写回答

1条回答 默认 最新

  • douzengjian1535 2017-01-27 09:21
    关注

    Try this by modifying core

    https://github.com/bcit-ci/CodeIgniter/blob/develop/system/database/DB_query_builder.php#L2019

    Of function

    public function set_update_batch($key, $index = '', $escape = NULL)
    {
    ..
    ..
    ..
    }
    

    From

    'value'=>($escape === FALSE ? $v2 : $this->escape($v2))
    

    To

    'value'=>(is_array($v2) ? $v2[0] : ($escape === FALSE ? $v2 : $this->escape($v2)))
    

    and give input like below

    $data = array(
       array(
          'fundCategoryId' => '3' ,
          'fundCategoryValue' => '500' , // generates with single quotes
    
       ),
       array(
          'fundCategoryId' => '4' ,
          'fundCategoryValue' => array('fundCategoryValue + 100') // generates without single quotes
    
       )
    );
    

    On old CI 2.2.x

    Modify DB_active_rec.php

    From

                if ($escape === FALSE)
                {
                    $clean[$this->_protect_identifiers($k2)] = $v2;
                }
                else
                {
                    $clean[$this->_protect_identifiers($k2)] = $this->escape($v2);
                }
    

    To

     $clean[$this->_protect_identifiers($k2)] = (is_array($v2)? $v2[0] : ( ($escape === FALSE) ? $v2 : $this->escape($v2) ));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?