• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Active Record handling of "where" and "or"

[eluser]Jonas G[/eluser]

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

My function looks like this:
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

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)

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

[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.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.