问题
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