【问题标题】:Is it a Active Record Class where(), like() limitation?它是一个 Active Record 类 where(), like() 限制吗?
【发布时间】:2011-08-18 00:45:41
【问题描述】:

我有一个关于 Active Record 类使用的问题,下面是我的代码 sn-p。

$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() 将无法再次工作。它将显示包括$关键字在内的所有年份记录。

是 Active Recrod 类的限制,还是我没有找到正确的方法来绑定 where() 和 like()。

感谢您的回复

【问题讨论】:

  • 您是否尝试过 codeigniter 活动记录是否允许链接(我怀疑但尝试),例如:$this->db->where(...)->like(...);
  • 我还没试过。我想尝试一下并稍后更新。谢谢。
  • @hakre,我试过$year = 2010; $this->db->where('year', $year)->like('name', $keyword)->or_like('chiptime', $keyword)->or_like('racenumber', $keyword); // chaining style。有用!您的评论正是正确的答案。谢谢。 `
  • 很高兴听到它对您有用,我将其添加为答案。

标签: php codeigniter activerecord php-5.3


【解决方案1】:

我认为这是“和”和“或”部分。目前您的 WHERE 如下所示:

WHERE 年份 = 2010 AND rank = 1 OR name = 'Peter'

由于您在那里有 OR,它会找到名称为“Peter”且排名为 1 且年份为 2010 的所有内容。

所以你应该更好地定义你的位置,并在需要的地方使用圆括号。

【讨论】:

  • 只需自己输入查询并使用 CI 提供的占位符。也更具可读性。这是 SQL 的原因
【解决方案2】:

你试过用codeigniter active record method chaining吗?喜欢:

$this->db->where(...)->like(...);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-11
    • 2015-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-18
    • 2013-02-27
    • 2013-09-26
    相关资源
    最近更新 更多