Parenthesis in ActiveRecord queries ? - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21) +--- Thread: Parenthesis in ActiveRecord queries ? (/showthread.php?tid=15739) |
Parenthesis in ActiveRecord queries ? - El Forum - 02-13-2009 [eluser]oll[/eluser] Hello, I have a videos table with fields like 'title', 'owner', 'description'. I have a little search engine that does a simple research ($search string) amongst these fields. Code: $fields = array('title','owner','description'); It works. But now, I need to secure my site. Each video belongs to one group (the table contains a 'group' field). An authenticated user belongs to several group ($groups array). And I want the search engine to display only the videos if he belongs to the group. So, I simply added a IN statement : Code: $fields = array('title','owner','description'); A query looks like : SELECT * FROM (`videos`) WHERE `groupe` IN ('public','linux','admin') AND `title` LIKE '%bird%' OR `owner` LIKE '%bird%' OR `description` LIKE '%bird%' But since AND has higher precedence than OR, it's exactly the same as : SELECT * FROM videos WHERE ( groupe IN (public,linux,admin) AND title LIKE bird ) OR owner LIKE bird OR description LIKE bird And I want of course : SELECT * FROM videos WHERE groupe IN (public,linux,admin) AND ( title LIKE bird OR owner LIKE bird OR description LIKE bird ) Is there an elegant way to have this result using CI active record queries ? I just need to add a couple of parenthesis. Or, at least, is there a way I can get the generated query, instead of running it ? For instance, something like : Code: $part1 = $this->db->display_or_like($field,$search); So that I can do something like : Code: $query ="(".$part1.")".$part2; Thank you. Parenthesis in ActiveRecord queries ? - El Forum - 03-15-2009 [eluser]brookerrj[/eluser] I would like to perform the the same kind of query in active record: (A AND B) OR (C AND D) Is there an elegant way? Parenthesis in ActiveRecord queries ? - El Forum - 03-16-2009 [eluser]m4rw3r[/eluser] I've made a library for this, called IgnitedQuery. It can handle parentheses and also subqueries, and is also somewhat backwards compatible with CI's AR (dunno how much, but a lot). Example: Code: // this is all PHP 4 (chaining can be used with PHP 5, use end() to end a subquery/parenthesis) (It is a part of IgnitedRecord, but also possible to use as a stand alone lib) Parenthesis in ActiveRecord queries ? - El Forum - 03-19-2009 [eluser]oll[/eluser] This is exactly what I needed. Thanks ! Parenthesis in ActiveRecord queries ? - El Forum - 07-13-2010 [eluser]mikedfunk[/eluser] I just found that when you need to put a portion of your where clause in parentheses, you can jump out of active record JUST for that portion and continue on with your other WHEREs and LIKEs after that. Code: // set prefixes Hopefully that's useful to someone. Parenthesis in ActiveRecord queries ? - El Forum - 08-26-2010 [eluser]atlanta[/eluser] [quote author="mikedfunk" date="1279077168"]I just found that when you need to put a portion of your where clause in parentheses, you can jump out of active record JUST for that portion and continue on with your other WHEREs and LIKEs after that. Code: // set prefixes Hopefully that's useful to someone.[/quote] Thanks my man that was very useful for me I was about to pull my hair out. much appreciated. |