Agile Toolkit - OR statement in combination with Model

♀尐吖头ヾ 提交于 2019-12-13 05:12:47

问题


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?


回答1:


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)
);


来源:https://stackoverflow.com/questions/17930334/agile-toolkit-or-statement-in-combination-with-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!