2013-05-28 35 views
2

我想更新数据库中的多行,并使用codeigniters update_batch()函数。
字段中指定的位置也应该更改。Codeigniter update_batch()包含更新的Where键

下面的代码应该清楚:在token指定

$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'); 

令牌应该device'none'token本身应设置为空字符串''进行更新。

这可能与update_batch()功能?


在SQL中我会写类似

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

为一个更新,但这是不可行的倍数,所以我想用update_batch()功能。

+1

你尝试了吗?什么是错误? – tomexsans

+0

@tomexsans我没有尝试,因为我不知道如何定义“new_token_value”。但是我不能在同一个数组中添加2次'token',使用实际的令牌值,而另一个使用新的值。 –

+0

那么更新批处理不是问题,问题在于如何根据需要更新数据。 – tomexsans

回答

0

我创建了一个与codeigniter batch_update()函数大致相同的帮助函数。
但有能力更新索引本身。新值由index_update_key定义。

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) 
0
$this->db->where('option1', $option1);<br/> 
$this->db->update_batch('table_name', $data, 'option2');