在codeigniter中为where每个查询添加where子句

时间:2018-03-28 15:28:57

标签: php mysql codeigniter activerecord

我有一种情况,我已经更新了所有数据库表,以包含一个名为“siteid”的新字段。因此,我想在codeigniter生成的每个查询中执行where子句。

如果不在每个模型中手动添加where子句,有没有办法做到这一点? 这是一个例子

$this->db->select('username');    
$this->db->where('userid');
$this->db->get('user');
$query->result_array();

我想为每个查询添加一个新的where子句,以便它像这样工作

$this->db->select('username');    
$this->db->where('userid');
$this->db->where('siteid');
$this->db->get('user');
$query->result_array();

2 个答案:

答案 0 :(得分:2)

在core /文件夹中创建MY_Model.php,并使所有模型扩展为MY_Model。

然后添加以下行:

$this->db->where('siteid');

进入MY_Model.php的构造函数。

有关扩展核心类的更多信息,请访问here

答案 1 :(得分:1)

如果不手动操作,我不确定是否有办法。

您可以使用搜索并将当前方法替换为搜索位置的关联数组方法。 Documentation link

搜索

$this->db->where('userid');

并替换为

$this->db->where(['userid' => $userid, 'siteid' => $siteid]);
相关问题