Codeigniter - 具有多个 Where 条件的批量更新 [英] Codeigniter - Batch Update with Multiple Where Conditions

查看:52
本文介绍了Codeigniter - 具有多个 Where 条件的批量更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于初学者来说,关于 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 命令添加多个 where 条件?

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

当我尝试此代码时,出现以下错误:

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:wampwwwwheelsystemdatabaseDB_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')

第一个参数将包含表名,第二个是值的关联数组,第三个参数是 where 键.

The first parameter will contain the table name, the second is an associative array of values, the third parameter is the where key.

推荐答案

您不能向 update_batch() 添加多个 where 子句.它只接受一个字符串作为 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.

来自来源:

/**
 * 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆