用户
 找回密码
 入住 CI 中国社区
搜索
查看: 2946|回复: 0
收起左侧

CI使用update时like条件失效的解决办法

[复制链接]
发表于 2013-3-19 14:04:51 | 显示全部楼层 |阅读模式
今天在用CI编写一个功能时使用update将指定的值加1,
下面是代码 :
PHP复制代码
 
public function where($where = false)
    {
        if ($where) {
            $this->where = $where;
        }
 
        if ($this->where) {
            if (is_array($this->where)) {
                foreach ($this->where as $key => $value) {
                    switch ($key) {
                        case 'start_date':
                            $this->db->where('submit_time >',$value);
                            break;
                            $this->db->like('title', $value);
                            break;
                        default:
                            $this->db->where($key,$value);
                            break;
                    }
                }
            }
           
            if (is_string($this->where)) {
                $this->db->where($where);
            }
        }
    }
public function set($where=false, $data)
    {
 
        $val = array();
        if (isset($data['export']) && $data) {
            $this->db->set('export',$data['export'],false);
        }
        $this->where($where);
        $this->db->ar_where=array_merge($this->db->ar_where,$this->db->ar_like);//将like条件与where合并
        return $this->db->update($this->_tableName);
    }
 
复制代码

看完代码也许你就明了了。我将ar_where与ar_like合并后,update就能正常使用like条件了。
下面是update中生成sql的代码,很明显,它并没有将ar_like作为查询条件。
PHP复制代码
$sql = $this->_update($this->_protect_identifiers($table, TRUE, NULL, FALSE), $this->ar_set, $this->ar_where, $this->ar_orderby, $this->ar_limit);
复制代码

http://www.zhangweijie.net/?p=839

本版积分规则