Search code examples
atk4

Agiletoolkit: Or'ing ands


I have a query that gives me a number of pairs of context_id and class_id. I want one of those to be met by another query. So I need to do where (co1 and cl1) or (co2 and cl2) or...

This one gives me a series of and's:

foreach($tscarr as $tsc)
{
  $grid->dq->where('context_id',$tsc['context_id'])->where('class_id',$tsc['class_id']);
}

I know from this page: http://agiletoolkit.org/doc/dsql/where that I should be able to do or. But I get an error when trying to use or in this way:

$grid->dq->where($grid->dq->or()->where($grid->dq->where('context_id',$tsc['context_id'])->where('class_id',$tsc['class_id'])));

Hope you have a good idea how to solve this...


Solution

  • You can use $dsql->orExpr() method.

    There are multiple examples in Stackoverflow like this one https://stackoverflow.com/a/17958175/1466341 and also in ATK4 Google groups here https://groups.google.com/forum/#!forum/agile-toolkit-devel just search for orExpr.

    I guess in your case something like this should work (untested):

    $q = $grid->dq;
    $or = $q->orExpr();
    
    foreach($tscarr as $tsc)
    {
        $or->where(
            $q->andExpr()
                ->where('context_id', $tsc['context_id'])
                ->where('class_id',   $tsc['class_id'])
        );
    }
    $q->where($or);