Welcome Guest, Not a member yet? Register   Sign In
Building a sql statement with the Active Record Class
#1

[eluser]PeterPan[/eluser]
Hi,

I used CI for some month now and I am getting used to it... slowly Wink

There is one thing I am not sure about whether it is a mistake by the built in Active Record Class or by me.

The where/orwhere statements are always grouped, so it is impossible to write such things as:

SELECT *
FROM table
WHERE a = b
AND statement LIKE &#xwh;atsoever%
OR a = b
AND statement LIKE &#xan;ythingelse%;

Using the code
Code:
$this->db->from('table');

$this->db->where('a', 'b');
$this->db->like('username', $filter);

$this->db->orlike('email', $filter);
$this->db->where('a', 'b');

$query = $this->db->get();

or even

Code:
$this->db->from('table');

$this->db->where('a', 'b');
$this->db->like('username', $filter);

$this->db->orwhere('a', 'b');
$this->db->like('email', $filter);

$query = $this->db->get();

the Active Record Class groups the WHERE statements together and combines my LIKE statements with an AND ;(

Thanks in advance!
#2

[eluser]Phil Sturgeon[/eluser]
Yes this is well known issue with Active Record. It sadly has no way of workout out where and how your commands should be grouped. This can be solved with the code below.

Code:
$this->db->from('table');

$this->db->where('(a = b AND username LIKE %'.$filter.'%'));
$this->db->orwhere('(a = b AND email LIKE %'.$filter.'%'));

$query = $this->db->get();

Its not as pretty, but hell.
#3

[eluser]PeterPan[/eluser]
Thank you! Wink




Theme © iAndrew 2016 - Forum software by © MyBB