开发框架 \ Yii \ yii2 andWhere 报错 $or/$and/$nor entries need to be full objects

yii2 andWhere 报错 $or/$and/$nor entries need to be full objects

总点击1109
简介:场景:   一个有点复杂的sql 需要使用的或的语句, 所以使用andwhere避免和where相互影响

场景: 

  一个有点复杂的sql  需要使用的或的语句,  所以使用andwhere 避免和where相互影响


分析:

    1. 首先$or 用法出了问题


       

$and_where = [

'$or',// 错误

['status_report' => 1],

['status' => 0,'status_report' => ['$exists' => false]]

];$and_where = [

'$or' => [ //正确

['status_report' => 1],'status_report' => ['$exists' => false]]

]

];


      2. andWhere 参数是不可以为空的

解决:

     andWhere 函数源码单独拿出来,加上为空的判断就好了   

$and_where = [

'$or' => [

['status_report' => 1],'status_report' => ['$exists' => false]]

]

];if ($and_where) {

$conditions = ['and',$conditions,$and_where];

}    


0人推荐

推荐文章

热门标签

意见反馈 常见问题 官方微信 返回顶部