Welcome Guest, Not a member yet? Register   Sign In
Active Record: how do I combine two or_like with an AND between them?
#2

[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%')
I'm afraid that it is not possible to do that with CI's AR.

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
// (but the escaping problems of 1.7 aren't in IQ, although it may escape somewhat different)
$query = new IgniedQuery();

if (isset($_POST['region'])) {
    // create an instance for the nested wheres
    $q = new IgnitedQuery();

    foreach ($_POST['region'] as $r)  {
        $searchstring = ','. $r .',';
        $q->or_like('regions', $searchstring);
    }

    // assign it to the WHERE part of the query
    $query->where($q);
}
if (isset($_POST['instrument'])){
    $q2 = new IgnitedQuery();

    foreach ($_POST['instrument'] as $i)  {
        $searchstring = ','. $i .',';
        $q2->or_like('instruments', $searchstring);
    }

    $query->where($q2);
}

$result = $query->get();
IgnitedQuery lib file in IgnitedRecord trac (download link on the bottom)


Messages In This Thread
Active Record: how do I combine two or_like with an AND between them? - by El Forum - 11-30-2008, 11:14 AM



Theme © iAndrew 2016 - Forum software by © MyBB