如何在codeigniter中构建活动记录查询?

时间:2012-10-20 07:19:01

标签: php codeigniter activerecord

我最近开始将我的codeigniter应用程序中的后端查询转换为活动记录模式。截至目前我正在使用codeigniter 2.1版本。我最初在mysql中编写了一个查询

SELECT * FROM table WHERE (field1 = $field1 OR field2 = $field2) AND field3 = $field3 AND field4 = 'text'

现在我想以下面给出的方式写这个东西:

$this->db->select('*');
$this->db->from('table');
$this->db->where('field1',$field1);
$this->db->or_where('field2',$field1);
$this->db->where('field3',$field2);
$this->db->where('field4','text');

但不知何故,“or_where”功能无法正常工作。对此有任何解决方案,我会很高兴吗?

1 个答案:

答案 0 :(得分:2)

您在Active Record

中编写自定义字符串
$where = "(field1 = " . $field1 . " OR field2 = " . $field2 . ") AND 
          field3 = ". $field3 . " AND field4 = 'text'";
$this->db->where($where);

Click here for source: Active Record (and browse for custom string)