Active Record: how do I combine two or_like with an AND between them? |
[eluser]sparbier[/eluser]
Hi all, I need to run a query that takes inputs from two arrays (from post data, don't know beforehand how many elements are given), and looks for the elements of these arrays in two separate fields in my database. So this is what I did: Code: if (isset($_POST['region'])) { Is there any way to achieve this with Active Records?
[eluser]m4rw3r[/eluser]
You mean that your SQL should look something like this? Code: WHERE (regions LIKE '%a%' OR regions LIKE '%b%') AND (instruments LIKE '%c%' OR instruments LIKE '%d%') But I have developed a SQL-builder which is capable to do this: IgnitedQuery. It is an improved version of CI's AR (a rewrite) which also is capable of handling subqueries and nested wheres (which you want in this case). I don't have much information available because I need to rewrite IgnitedRecord's manual (which includes IgnitedQuery's, because IgnitedRecord uses IgntedQuery to generate SQL). Your example would look like this with IQ: Code: // $query can be treated just like CI's AR
[eluser]sparbier[/eluser]
thanks m4 for your help! seems like your lib is a really worthwile extension to CI, maybe they'll take that as an example to integrate something likewise in coming CI versions :-) Meanwhile, as I had to deliver this weekend, I opted to builde the query string directly and then use query() rather than the active records stuff. May get me into trouble if I ever change to a db (currently I'm on mysql) with different syntax, but currently it works fine. thx again!
[eluser]m4rw3r[/eluser]
Of course you can do it yourself (I have nothing against hardcoded SQL), but just remember to escape properly |
Welcome Guest, Not a member yet? Register Sign In |