Reputation: 10061
I want to find all records from user table except one user type. i.e. I have a user table dec_user
where there is an attributes user_type
. I want to find all records except user_type
9
. Then I'll count the number of rows. So, I wrote as:
$user_type = 9;
return count(User::model()->findAll(array("condition"=>"':user_type' != $user_type")));
Actually, I do not understand how to write this condition.
Upvotes: 1
Views: 5854
Reputation: 79032
You do not need to retrieve an array from database and count it using the PHP count()
function.
The Yii way:
return User::model()->count('user_type <> '.$user_type);
or by using params:
return User::model()->count('user_type <> :type', array('type' => $user_type);
or, if you want to build the SQL query, use commandBuilder:
return Yii::app()->db->createCommand()
->select('COUNT(*)')
->from('user')
->where('user_type <> '.$user_type)
->queryScalar();
Upvotes: 7