![]() |
using or_where() on same field - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: using or_where() on same field (/showthread.php?tid=30655) Pages:
1
2
|
using or_where() on same field - El Forum - 05-21-2010 [eluser]blorriman[/eluser] I am trying to to a where(), or_where() on the same field (groups_members.resign_date). I want to find members who have either not resigned (field is NULL), or who resigned after the start_date. Code: $this->db->where('groups_members.join_date <=', $end_date); It's not working and I can't seem to find an answer - anyone have a suggestion? using or_where() on same field - El Forum - 05-21-2010 [eluser]WanWizard[/eluser] After the query, put Code: echo $this->db->last_query(); The issue is probably that you need to add brackets when mixing AND's and OR's in a WHERE clause. Which Active Record can't do. using or_where() on same field - El Forum - 05-21-2010 [eluser]blorriman[/eluser] I'm not sure why, but by putting the or_where() before the where() seems to work. Code: $this->db->where('groups_members.group_id', $this->session->userdata('group_id')); using or_where() on same field - El Forum - 05-21-2010 [eluser]WanWizard[/eluser] It works, but probably not the way you intend, as the query is still assembled without brackets. So you query becomes something like "WHERE A = 1 AND B = 2 AND C = 3 OR D = 4". Which means that as long as D = 4 the row is selected, all the other fields are ignored. Probably not what you intended. using or_where() on same field - El Forum - 05-21-2010 [eluser]blorriman[/eluser] Well, this is what I'm getting, and it seems to work for members who have not resigned (resign_date IS NULL) as well as for members who resigned after the start_date. Code: SELECT * FROM (`members`) using or_where() on same field - El Forum - 05-21-2010 [eluser]WanWizard[/eluser] Exactly. Which would return all records in which group_members.resign_date IS NULL, regardless of the other values. using or_where() on same field - El Forum - 05-21-2010 [eluser]blorriman[/eluser] You're right, so are you saying this can't be done in Active Record Class? using or_where() on same field - El Forum - 05-21-2010 [eluser]WanWizard[/eluser] Correct. Depending on the type of query some tricks work, but you loose automatic escaping and table prefixing, so imho not worth the trouble. If you dare to modify CI's DB_active_rec.php (I have it extended, but default CI doesn't support that), you can replace the where() and or_where() method by these: Code: // -------------------------------------------------------------------- using or_where() on same field - El Forum - 05-21-2010 [eluser]blorriman[/eluser] Thanks WanWizard, I'll take a look at this. using or_where() on same field - El Forum - 05-21-2010 [eluser]WanWizard[/eluser] Note that this doesn't work on the first ->where() of your query. To fix that I have to find a bit more time, as that involves a lot more changes, which means a better solution than this hack must be found. |