场景:
一个有点复杂的sql 需要使用的或的语句, 所以使用andwhere 避免和where相互影响
分析:
1. 首先$or 用法出了问题
$and_where = [ '$or', // 错误 ['status_report' => 1], ['status' => 0, 'status_report' => ['$exists' => false]] ];
$and_where = [ '$or' => [ //正确 ['status_report' => 1], ['status' => 0, 'status_report' => ['$exists' => false]] ] ];
2. andWhere 参数是不可以为空的
解决:
andWhere 函数源码单独拿出来, 加上为空的判断就好了
$and_where = [ '$or' => [ ['status_report' => 1], ['status' => 0, 'status_report' => ['$exists' => false]] ] ];
if ($and_where) { $conditions = ['and', $conditions, $and_where]; }