I can’t create the following query using Yii:
SELECT recipientId as broadcasterId, SUM(quantity) as quantity FROM `creditlog`
WHERE websiteId=3 AND timeAdded>='2013-01-17'
AND timeAdded<='2013-02-17'
AND recipientId IN (10000024, 10000026, 1000028) GROUP BY `recipientId`
I tried:
$command = Yii::app()->db->createCommand();
$command->select('recipientId as broadcasterId, SUM(quantity) as quantity');
$command->from('creditlog');
$command->where('websiteId=:websiteId AND timeAdded>=:dateStart AND timeAdded<=:dateEnd AND recipientId IN (:recipients)',array(':websiteId' => $websiteId, ':dateStart' => $dateStart, ':dateEnd' => $dateEnd, ':recipients' => $broadcasterIds));
$command->group('recipientId');
also the andWhere() function which is in the docs seems to be missing.
The issue is that IN condition but I can’t find a way to rewrite it.
Since you don’t have access to
andWhere, which would make life much simpler, you have to express the parameters withwherelike this:This is done so that you can at some point use the proper
array('in', 'recipientId', $values)syntax to produce theIN(...)SQL.However, this is ugly and difficult to manage. As long as all the conditions are simply joined together with
ANDyou can construct the data structure programmatically from a saner data representation like this (in effect this is a workaround for the missingandWhere):For more information on why this way of expressing things has to be used you can refer to the documentation for
CDbCommand::where.