计算在Yii中查找所有记录

Str*_*der 1 php yii

我想查找用户表中除一个用户类型以外的所有记录.即我有一个用户表dec_user,其中有一个属性user_type.我想找到除了以外的所有记录user_type 9.然后我会计算行数.所以,我写道:

$user_type = 9;
    return count(User::model()->findAll(array("condition"=>"':user_type' != $user_type")));
Run Code Online (Sandbox Code Playgroud)

实际上,我不明白如何写这个条件.

Sam*_*iew 6

您不需要从数据库中检索数组并使用PHP count()函数对其进行计数.

Yii方式:

return User::model()->count('user_type <> '.$user_type);
Run Code Online (Sandbox Code Playgroud)

或使用params:

return User::model()->count('user_type <> :type', array('type' => $user_type);
Run Code Online (Sandbox Code Playgroud)

或者,如果要构建SQL查询,请使用commandBuilder:

return Yii::app()->db->createCommand()
            ->select('COUNT(*)')
            ->from('user')
            ->where('user_type <> '.$user_type)
            ->queryScalar();
Run Code Online (Sandbox Code Playgroud)