2

I got a question on Active Record Class usage, my code snippet below.

$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); 

When I bind $this->db->like() after $this->db->where() in my Active Recrod Class, the $this->db->where() wont’ work again. It will show all the years record that including the $keyword.

Is it a limitation in Active Recrod Class, or I didn’t find a right way to bind where() and like().

Appreciated for your replies

Nano HE
  • 1,879
  • 7
  • 28
  • 39
  • 1
    Have you tried if codeigniter active record allows chaining (I doubt it but try), like: `$this->db->where(...)->like(...);`? – hakre Aug 18 '11 at 00:53
  • I haven't tried yet. and I would like to try it and update later. Thanks. – Nano HE Aug 18 '11 at 00:57
  • @hakre, I tried `$year = 2010; $this->db->where('year', $year)->like('name', $keyword)->or_like('chiptime', $keyword)->or_like('racenumber', $keyword); // chaining style`. It works! Your comment are exactly the right answer. Thanks. ` – Nano HE Aug 19 '11 at 00:01
  • Nice to hear it worked for you, I added it as an answer. – hakre Aug 19 '11 at 11:15

2 Answers2

5

I think it's the "and" and "or" part. Currently your WHERE looks like this:

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

Since you got an OR there, it will find everything with the name "Peter" or with rank is 1 and year is 2010.

So you should define your where better and use round brackets where needed.

P.T.
  • 3,130
  • 4
  • 19
  • 24
  • Just type the query yourself and use the placeholders CI provides. Much more readable too. It's SQL for a reason – Rudie Aug 19 '11 at 16:55
2

Have you tried to use codeigniter active record method chaining?; like:

$this->db->where(...)->like(...);
hakre
  • 193,403
  • 52
  • 435
  • 836