Welcome Guest, Not a member yet? Register   Sign In
Parentheses and ActiveRecord
#1

[eluser]davido[/eluser]
I have code like this:
Code:
$this->db->like('fname', $name[0]);
$this->db->or_like('lname', $name[0]);
//AND
$this->db->where("submitted BETWEEN '$begin' and '$end'");
$this->db->or_where("processed BETWEEN '$begin' and '$end'");
Making my query:
Code:
SELECT * FROM users WHERE submitted BETWEEN '2009/1/1' AND '2009/12/31' OR processed BETWEEN '2009/1/1' AND '2009/12/31' AND fname LIKE '%paul%' OR lname LIKE '%paul%'

The problem is I need to have ors have precedence over the ands. I can do this with my dates by having the first AR function start with a paren and the second end with a paren, but doing so on the names would require lots of rewriting of code and I think there should be a better way. So how would I get a query like this in Active Records?
Code:
SELECT * FROM users WHERE (submitted BETWEEN '2009/1/1' AND '2009/12/31' OR processed BETWEEN '2009/1/1' AND '2009/12/31') AND (fname LIKE '%paul%' OR lname LIKE '%paul%')
#2

[eluser]Armchair Samurai[/eluser]
As of 1.7.1, AR doesn't do brackets, so you need to do it the hard way.
Code:
$name[0] = $this->db->escape("%$name[0]%");
$begin = $this->db->escape($begin);
$end = $this->db->escape($end);

$this->db->where("(fname LIKE $name[0] OR lname LIKE $name[0])", NULL, FALSE);
$this->db->where("(submitted BETWEEN $begin AND $end OR processed BETWEEN $begin AND $end)", NULL, FALSE);
#3

[eluser]Mackstar[/eluser]
Hi Armchair Samurai,

Interesting to see you are in Osaka, I am in Nishinomiya just down the road, doing web dev here (mostly php and rails), if I ever have extra work is it worth keeping you in mind???

Cheers

Richard




Theme © iAndrew 2016 - Forum software by © MyBB