Welcome Guest, Not a member yet? Register   Sign In
Rearrange active record querys?
#1

[eluser]skylerrichter[/eluser]
I have a query I am trying to do with the active records class but I am getting some unexpected results here is the model:


Code:
$this->db->select('id, first_name, last_name, email, status, industry');
$this->db->or_like(array('first_name' => $string, 'last_name' => $string));
$this->db->where('status', 1);
$this->db->where_in('id', $this->get_friends());
            
$query = $this->db->get('users');
                                                
if($query->num_rows() > 0)
{
    return $query->result_array();
}
else
{
    return false;
}

Here is the result of the query (close but no cigar) :

Code:
SELECT `id`, `first_name`, `last_name`, `email`, `status`, `industry` FROM (`users`) WHERE `status` = 1 AND `id` IN ('66', '41')  AND  `first_name`  LIKE '%a%' OR  `last_name`  LIKE '%a%'

The LIKE statement needs to appear in brackets (`first_name` LIKE '%a%' OR `last_name` LIKE '%a%')

And the WHERE IN statement needs to appear last in order to get the expected results IN ('66', '41')

Ideally the correct SQL should look like this:

Code:
SELECT `id`, `first_name`, `last_name`, `email`, `status`, `industry` FROM (`users`) WHERE `status` = 1 AND (  `first_name`  LIKE '%a%' OR  `last_name`  LIKE '%a%') AND `id` IN ('66', '41')

Any ideas?
#2

[eluser]wabu[/eluser]
How about just writing a custom where clause (still supported by where(), see docs)?




Theme © iAndrew 2016 - Forum software by © MyBB