CodeIgniter Forums
Active Record handling of "where" and "or" - 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: Active Record handling of "where" and "or" (/showthread.php?tid=11273)



Active Record handling of "where" and "or" - El Forum - 09-02-2008

[eluser]Jonas G[/eluser]
Hello

I am making an active record database call that uses a lot of "or_like" and a single "where".

My function looks like this:
Code:
function filterSearch($q) {
        $this->db->join('groups', 'groups.group_id = ccards.group_id');
        $field_names = $this->db->list_fields('ccards');
        foreach ($field_names as $field_name) {
            if ($field_name != "institution_id" && $field_name != "group_id") {
                $this->db->or_like($field_name, $q);
            }
        }
        $this->db->or_like('groups.name', $q);
        $this->db->where('ccards.institution_id', $this->session->userdata('institution_id'));
        
        $query = $this->db->get('ccards');
        
        return $query;
    }

I only want results that have ccards.institution_id = userdata('institution_id'), yet all the or_like make sql return results that have different institution_id as well.

So my question is: how do make the call so the where clause get "highest priority"

All help is greatly appreciated

Jonas G


Active Record handling of "where" and "or" - El Forum - 09-02-2008

[eluser]xwero[/eluser]
This required parenthesis but that isn't possible with the current AR library so an option is to create the where part on your own. It should look like this when generated
Quote:WHERE ccards.institution_id = 1 OR (field_name = 2 OR field_name = 3)



Active Record handling of "where" and "or" - El Forum - 09-02-2008

[eluser]m4rw3r[/eluser]
The SQL-generator in IgnitedRecord (IgnitedQuery) is capable of this, see IgnitedQuery code.
It works as a standalone lib too.


Active Record handling of "where" and "or" - El Forum - 09-04-2008

[eluser]Jonas G[/eluser]
Thanks for your help guys. I ended up going with xwero's solution to generate the sql. I will try looking into IgnitedQuery at another time. Again: thank you for your help.