Yii Query Builder multiple where clauses

对着背影说爱祢 提交于 2020-01-24 20:17:06

问题


I noticed in my stack that my query wasn't being executed correctly as I had multiple where clauses in my querybuilder. So I looked at this post Multiple call where in Yii Query builder

Applied what i'd read, but still the query doesn't combine the where statements. What am I doing wrong?

            $command = Yii::app()->db->createCommand()
        ....
        ->where(array('in', 'u.id', $licenses), array('and', 'i.date_added > DATE_SUB(u.date_expired, INTERVAL  30 DAY)'));
        //->where(array('and', 'i.date_added > DATE_SUB(u.date_expired, INTERVAL  30 DAY)'));
        //->where(array('and', 'u.date_expired > CURDATE()'))
        ->group('u.id');

These were 3 individual statements, but I combined them as I read, but still the same result. Only 1 where clause.


回答1:


You should use andWhere method. This method is putting the AND keyword so you juste need the condition:

$command = Yii::app()->db->createCommand()
    ....
    ->where(array('in', 'u.id', $licenses));
    ->andWhere('i.date_added > DATE_SUB(u.date_expired, INTERVAL  30 DAY)');
    ->andWhere('u.date_expired > CURDATE()')
    ->group('u.id');


来源:https://stackoverflow.com/questions/18668553/yii-query-builder-multiple-where-clauses

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