它是一个活动记录类,其中(),如()限制?

时间:2011-08-18 00:45:41

标签: php codeigniter activerecord php-5.3

我在Active Record Class使用方面遇到了一个问题,我的代码片段如下。

$this->db->select('year, distance, gender, rank, name, chiptime, racenumber');
$this->db->order_by("year", "desc");
$this->db->order_by("distance, gender, rank", "asc");
$year = 2010;    
$this->db->where('year', $year);   // where() doesn't work!
$this->db->like('rank', $keyword); // Assume I didn't add like() with $keywords, where() works well.
$this->db->or_like('name', $keyword); 

当我在我的Active Recrod类中$ this-> db-> where()之后绑定$ this-> db-> like()时,$ this-> db-> where()不会再工作了。它将显示包含$ keyword的所有年份记录。

它是Active Recrod Class的限制,还是我找不到绑定where()和like()的正确方法。

赞赏你的回复

2 个答案:

答案 0 :(得分:5)

我认为这是“和”和“或”部分。目前你的WHERE看起来像这样:

WHERE year = 2010 AND rank = 1 OR name ='Peter'

由于你有一个OR,它会找到名称为“Peter”的所有内容,排名为1,年份为2010年。

因此,您应该更好地定义您的位置,并在需要时使用圆括号。

答案 1 :(得分:2)

您是否尝试过使用codeigniter active record method chaining?喜欢:

$this->db->where(...)->like(...);
相关问题