Codeigniter:将额外参数插入MySQL语句中

时间:2015-06-24 14:18:13

标签: php mysql codeigniter

我有一个包含多个函数的模型,其中一个函数应该从几个匹配指定标记号的表中删除一些记录。但是,当我将MySQL语句输出到错误日志时,我看到它正在添加一些额外的参数,这些参数似乎是从模型中的另一个函数中获取的。这很奇怪,我不能为我的生活找出正在发生的事情。

这是功能:

function deleteEquipment() {
    $tag = $this->uri->segment(3);
    $this->db->where('tag', $tag);
    $this->db->delete('equipment');
    error_log('mysql: '.$this->db->last_query());
    $this->db->where('tag', $tag);
    $this->db->delete('process_record');
    $this->db->where('tag', $tag);
    $this->db->delete('hold');
    $this->db->where('tag', $tag);
    $this->db->delete('dpo');
    error_log('mysql: '.$this->db->last_query());
    return;
}

以下是错误日志:

WHERE (archived<>'yes' AND (on_lot<>'yes' OR photographs_uploaded<>'yes') AND sold<>'yes') OR (archived<>'yes' AND sold='yes' AND (final_inspection<>'yes' OR jdlink2_registered<>'yes'))
AND `tag` =  '40'
[24-Jun-2015 16:10:21 Europe/Berlin] mysql: DELETE FROM `process_record`
WHERE `tag` =  '40'
[24-Jun-2015 16:10:21 Europe/Berlin] mysql: DELETE FROM `hold`
WHERE `tag` =  '40'
[24-Jun-2015 16:10:21 Europe/Berlin] mysql: DELETE FROM `dpo`
WHERE `tag` =  '40'

所以,最后三个语句看起来很好,但第一个语句中插入了所有这些额外的参数。我看到这些参数的唯一其他地方是同一模型中与分页相关的几个函数,例如:

function getRowCount() {
        // to do: edit this
        $q = $this->db->get('equipment');
        $where = "(archived<>'yes' AND (on_lot<>'yes' OR photographs_uploaded<>'yes') AND sold<>'yes') OR (archived<>'yes' AND sold='yes' AND (final_inspection<>'yes' OR jdlink2_registered<>'yes'))"; 
        $this->db->where($where);
        $rowcount = $q->num_rows();
        return $rowcount;       
    }

我不知道为什么来自完全独立的函数的参数出现在第一个MySQL语句中。注意:在同一模型中还有另一个功能发生相同的事情:对三个独立的表进行一些更新,其中行与标签号匹配。设备表查询引入了这个奇怪的东西,但其他查询看起来很好。

知道为什么会这样吗?

3 个答案:

答案 0 :(得分:2)

Active Record正在缓存WHERE中的getRowCount,因为您无法调用方法。

当来自deleteEquipment的{​​{1}}仍在Active Record缓存中时,您的应用程序会调用WHERE

getRowCount

答案 1 :(得分:1)

当您致电log4cplus.logger.business=ALL,BUSINESS log4cplus.additivity.business=false log4cplus.appender.STDOUT=log4cplus::ConsoleAppender log4cplus.appender.STDOUT.layout=log4cplus::PatternLayout log4cplus.appender.STDOUT.layout.ConversionPattern=%-6p[%t][%D{%m/%d/%y %H:%M:%S %Q}]%m log4cplus.appender.BUSINESS=log4cplus::RollingFileAppender log4cplus.appender.BUSINESS.File=./all.log log4cplus.appender.BUSINESS.MaxFileSize=5MB log4cplus.appender.BUSINESS.MaxBackupIndex=5 log4cplus.appender.BUSINESS.layout=log4cplus::PatternLayout log4cplus.appender.BUSINESS.layout.ConversionPattern=%-6p[%t] [%D{%m/%d/%y %H:%M:%S %Q}] %m 时,您运行查询。因此,在$this->db->get()函数中运行getRowCount(),然后设置where子句。

where子句已设置,但在下一个查询之前未使用,该查询位于SELECT * FROM equipment函数中。

你需要这样做:

deleteEquipment()

答案 2 :(得分:0)

在函数getRowCount()中,$ this-&gt; db-&gt; get必须是最后一个。它应该是这样的:

function getRowCount() {
        // to do: edit this            
        $where = "(archived<>'yes' AND (on_lot<>'yes' OR photographs_uploaded<>'yes') AND sold<>'yes') OR (archived<>'yes' AND sold='yes' AND (final_inspection<>'yes' OR jdlink2_registered<>'yes'))"; 
        $this->db->where($where);
        $q = $this->db->get('equipment');
        $rowcount = $q->num_rows();
        return $rowcount;       
    }

在您的代码中,因为$ this-&gt; db-&gt; where()后面没有$ this-&gt; db-&gt; get(),导致该条件仍然保留用于下一个查询。