user2367156 user2367156 - 5 months ago 18
SQL Question

How can add a condtion to Yii CDbCriteria select?

I use this codes.
I want check if budget < 0 select it.

$criteria = new CDbCriteria;
$criteria->select = array('*', 'SUM(amount) as budget');
$criteria->group = 'user_id';
$debtors = Transactions::model()->findAll($criteria);


How can i add this condition?

Answer

use

$criteria = new CDbCriteria;
$criteria->select = array('*', 'SUM(amount) as budget');
$criteria->having = 'budget > 0 ' ;
$criteria->group = 'user_id';
$debtors = Transactions::model()->findAll($criteria);