Welcome Guest, Not a member yet? Register   Sign In
how to use parethesis in active record specially 'where' clause ??
#1

[eluser]far032[/eluser]
SELECT * FROM tbl WHERE col1 = '$val' AND ( col2 = '$val1' OR col2 = '$val2' ..... OR col2 = '$valn');

Please help me how can i write this query in CI active record??
Main problem is that i can't use the parenthesis in CI active record.

( col2 = '$val1' OR col2 = '$val2' ..... OR col2 = '$valn') may be a array.

For example:

foreach($data as $row):
$this->db->where('col2',$row->val);
endforeach;

I was trying it

$this->db->select('*');
$this->db->from('tbl');
$this->db->where('col1',$val);
foreach($data as $row):
$this->db->or_where('col2',$row->val);
endforeach;

$this->db->get();
#2

[eluser]far032[/eluser]
Dear CI expert,

I need urgent help about this issue.

Regards
Faruque Rahman
#3

[eluser]WanWizard[/eluser]
For a dirty trick, see http://ellislab.com/forums/viewthread/163808/
#4

[eluser]petewulf[/eluser]
Hey Guys,

i don't know who is developing the Codeigniter project but every day i'm getting more and more thinking about changing the framework to another one. This feature request was very often posted, i just found a thread from 2008, this was 2 years ago! There are so much small and useful features missing which makes the life with CI really hard. The whole Active record class is useless if it's not possible to correctly set some brackets to get the correct database results. I#m afraid that the development of CI is getting stuck.

regards pete
#5

[eluser]cahva[/eluser]
Eh.. Its possible so whats the rant about? This is documented in the userguide and mentioned in the post that WanWizard gave in earlier post. Its not a hack. Using NULL as second parameter and FALSE as third parameter.. I dont know what is so hard about that?
#6

[eluser]petewulf[/eluser]
Hello cahva,

yes, its possible, your're right. But do you think this solution is implemented in a good manner? I don't want to get headache about writing complicated workarounds to use the active record class:

Code:
$this->CI->db->where('( 1 =', '1', false); // add a bracket open
$this->CI->db->where('1', '1 )', false); // add a backet close

...or writing the queries or query parts manually like this:

Code:
$this->db->where("(`username` = $param1 OR `email` = $param1)", NULL, FALSE);

So whats the benefit here using the active record class? Its much more faster and much more readable to write the query itself as a pure string, so i don't have to think about what had happened here a few days later. The second Example is a little but better, but not okay for a dedicated db-controller.

I simply want to give an array to the active record class and the class should build my query correctly. I just want to get my projects done without worrying about CI problems. A framework should ease the way of coding (although most of the time CI works pretty good).

Regards pete
#7

[eluser]CroNiX[/eluser]
The active record class was meant for basically simple queries. If you need more complex statements, just run them through $this->db->query(your ultra complex query);

Simple.




Theme © iAndrew 2016 - Forum software by © MyBB