atk4agiletoolkit

Agile Toolkit - OR statement in combination with Model


I have a Model from which I want to select all rows for which 'caller' or 'callee' is a given value for presentation in a Grid.

I have tried lots of different avenues of accomplishing this and cannot get anywhere with it.

I have a working filter on the Grid which narrows down the results by date (starting and ending dates), by status ("ANSWERED","NO_ANSWER"), I can also add conditions for 'caller' and 'callee', but how do I get it to show all rows where either 'caller' or 'callee' is a match to the current $UserID? Basically showing all calls (rows) a user was involved in?

The MySQL query itself is a simple OR construction, but how do I 'feed' it into the Model or the Grid so that it plays nicely with the other filters on the page?


Solution

  • You can use orExpr() method of DSQL to generate SQL for your needs.

    For example,

    $m = $this->model->debug();            // enable debug mode
    $user_id = $this->api->auth->model->id;// currently logged in user ID
    $q = $m->_dsql();                      // get models DSQL
    
    $or = $q->orExpr();                    // initialize OR DSQL object
    $or->where('caller', $user_id)         // where statements will be joined with OR
       ->where('callee', $user_id);
    
    // use one of these below. see which one works better for you
    $q->where($or);                        // add condition with OR statements
    $q->having($or);                       // add condition with OR statements
    

    Of course you can write all of this shorter:

    $m = $this->model->debug();            // enable debug mode
    $user_id = $this->api->auth->model->id;// currently logged in user ID
    $q = $m->_dsql();                      // get models DSQL
    
    $q->where($q->orExpr()
        ->where('caller', $user_id)
        ->where('callee', $user_id)
    );