Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Codeigniter update_batch(),包括where键的更新_Php_Codeigniter_Activerecord_Codeigniter 2 - Fatal编程技术网

Php Codeigniter update_batch(),包括where键的更新

Php Codeigniter update_batch(),包括where键的更新,php,codeigniter,activerecord,codeigniter-2,Php,Codeigniter,Activerecord,Codeigniter 2,我想用codeignitersupdate\u batch()函数更新数据库中的多行。 但是在where中指定的字段也应该更改 以下代码应明确说明: $set = array( array( 'token' => '65787131678754', 'device' => 'none', 'new_token_value' => '' ), array( 'token' =&g

我想用codeigniters
update\u batch()
函数更新数据库中的多行。
但是在where中指定的字段也应该更改

以下代码应明确说明:

$set = array(
  array(
    'token'           => '65787131678754',
    'device'          => 'none',
    'new_token_value' => ''
  ),
  array(
    'token'           => '75798451315464',
    'device'          => 'none',
    'new_token_value' => ''
  )
);

$this->db->update_batch(TBL_NAME, $set, 'token');
token
中指定的令牌应使用
device
更新为
'none'
,并且
令牌本身应设置为空字符串
'

使用
update\u batch()
功能是否可以实现这一点


在sql中,我会编写如下内容

UPDATE TBL_NAME
SET token='', device='none'
WHERE token='65787131678754'

对于一个更新,但对于多个更新,这是不可行的,因此我想使用
update\u batch()
函数

我创建了一个助手函数,该函数与codeigniter
batch\u update()函数基本相同。
但是有能力更新索引本身。新值由
索引\u更新\u键定义

function update_batch($db, $table = '', $set = NULL, $index = NULL, $index_update_key = '') {
if ($table === '' || is_null($set) || is_null($index) || !is_array($set)) {
    return FALSE;
}

$sql = 'UPDATE ' . $db->protect_identifiers($table) . ' SET ';

$ids = $when = array();
$cases = '';

//generate the WHEN statements from the set array
foreach ($set as $key => $val) {
    $ids[] = $val[$index];

    foreach (array_keys($val) as $field) {
        if ($field != $index && $field != $index_update_key) {
            $when[$field][] = 'WHEN ' . $db->protect_identifiers($index) 
                            . ' = ' . $db->escape($val[$index]) . ' THEN ' . $db->escape($val[$field]);
        } elseif ($field == $index) {
            //if index should also be updated use the new value specified by index_update_key
            $when[$field][] = 'WHEN ' . $db->protect_identifiers($index) 
                            . ' = ' . $db->escape($val[$index]) . ' THEN ' . $db->escape($val[$index_update_key]);
        }
    }
}

//generate the case statements with the keys and values from the when array
foreach ($when as $k => $v) {
    $cases .= "\n" . $db->protect_identifiers($k) . ' = CASE ' . "\n";
    foreach ($v as $row) {
        $cases .= $row . "\n";
    }

    $cases .= 'ELSE ' . $k . ' END, ';
 }

 $sql .= substr($cases, 0, -2) . "\n"; //remove the comma of the last case
 $sql .= ' WHERE ' . $index . ' IN (' . implode(',', $ids) . ')';

 return $db->query($sql);
}
现在我可以做以下事情了

$set = array(
  array(
    'token'           => '657871316787544',
    'device'          => 'none',
    'new_token_value' => ''
  ),
  array(
    'token'           => '757984513154644',
    'device'          => 'none',
    'new_token_value' => ''
  )
);

update_batch($this->db, 'table_name', $set, 'token', 'new_token_value');
sql输出是

UPDATE `b2c` SET 
`token` = CASE 
WHEN `token` = '657871316787544' THEN ''
WHEN `token` = '757984513154644' THEN ''
ELSE token END, 
`device` = CASE 
WHEN `token` = '657871316787544' THEN 'none'
WHEN `token` = '757984513154644' THEN 'none'
ELSE device END
WHERE token IN (657871316787544,757984513154644)
$this->db->where('option1',$option1)
$this->db->update_batch('table_name',$data,'option2');
您已经试过了吗?什么是错误?@tomexans我没有尝试,因为我不知道如何定义“新的令牌值”。但是我不能在同一数组中添加两次
令牌
,一次使用实际的令牌值,另一次使用新的值。那么更新批不是问题,问题是如何按照您希望的方式更新数据。那么我应该使用纯sql和每个令牌的循环来完成吗?但我认为这不是一个好的解决方案。您对如何更新多个入口有更好的想法吗?看看codeigniter事务。
$this->db->where('option1', $option1);<br/>
$this->db->update_batch('table_name', $data, 'option2');