codeigniter - 与多个where条件批量更新 [英] Codeigniter - Batch Update with Multiple Where Conditions
问题描述
对于初学者来说,在update_batch的codeigniter文件不存在。 kenjis还跟提供一些文档,并将其提交到仓库里。希望他们很快把它。
For starters, the Codeigniter documentation on update_batch does not exist. kenjis was kind enough to provide some documentation and submit it to the repository. Hopefully they pull it soon.
有谁知道如何添加多个有条件的地方要codeigniters update_batch命令?
Does anyone know how to add multiple where conditions to Codeigniters update_batch command?
我的期望用途:
$where = array(
'title',
'name'
);
$this->db->update_batch('mytable', $data, $where);
当我尝试这样做code,我得到了如下错误:
When I tried this code I got the follow error:
A Database Error Occurred
One or more rows submitted for batch updating is missing the specified index.
Filename: C:\wamp\www\wheel\system\database\DB_active_rec.php
Line Number: 1451
按kenjis更新批处理文件:
$this->db->update_batch();
基于您提供数据的更新字符串,并运行查询。您可以传递一个阵列或对象的功能。下面是一个使用数组的例子:
Generates an update string based on the data you supply, and runs the query. You can either pass an array or an object to the function. Here is an example using an array:
$data = array(
array(
'title' => 'My title' ,
'name' => 'My Name 2' ,
'date' => 'My date 2'
),
array(
'title' => 'Another title' ,
'name' => 'Another Name 2' ,
'date' => 'Another date 2'
)
);
$this->db->update_batch('mytable', $data, 'title');
// Produces:
// UPDATE `mytable` SET `name` = CASE
// WHEN `title` = 'My title' THEN 'My Name 2'
// WHEN `title` = 'Another title' THEN 'Another Name 2'
// ELSE `name` END,
// `date` = CASE
// WHEN `title` = 'My title' THEN 'My date 2'
// WHEN `title` = 'Another title' THEN 'Another date 2'
// ELSE `date` END
// WHERE `title` IN ('My title','Another title')
第一个参数包含表名,第二个是值的关联数组,第三个参数是关键的地方。
The first parameter will contain the table name, the second is an associative array of values, the third parameter is the where key.
- kenjis的文档更新: <一href="https://bitbucket.org/kenjis/ci-user-guide/changeset/3d579dd14afe">https://bitbucket.org/kenjis/ci-user-guide/changeset/3d579dd14afe
- saintnicster的拉力要求:<一href="https://github.com/EllisLab/$c$cIgniter/pull/448">https://github.com/EllisLab/$c$cIgniter/pull/448
- kenjis's documentation update: https://bitbucket.org/kenjis/ci-user-guide/changeset/3d579dd14afe
- saintnicster's pull request: https://github.com/EllisLab/CodeIgniter/pull/448
推荐答案
您不能添加多个where子句,以 update_batch()
。它只接受一个字符串作为where子句,所以我敢肯定,有没有办法做到这一点的方法目前正在撰写的方式,第三个参数。
You can't add multiple where clauses to update_batch()
. It only accepts a string as the third parameter for the where clause so I'm sure there's no way to do this the way the method is currently written.
在<一个href="https://github.com/EllisLab/$c$cIgniter/blob/develop/system/database/DB_active_rec.php">source:
/**
* Update_Batch
*
* Compiles an update string and runs the query
*
* @param string the table to retrieve the results from
* @param array an associative array of update values
* @param string the where key
* @return object
*/
public function update_batch($table = '', $set = NULL, $index = NULL)
这篇关于codeigniter - 与多个where条件批量更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!