简体   繁体   中英

count findAll records in Yii

I want to find all records from user table except one user type. ie 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.

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM