Yii : how to count records in a model?

Gunah Gaar picture Gunah Gaar · Dec 5, 2012 · Viewed 62k times · Source

I have following code to fetch data from a model.

$notifyModel = Notification::model()->findByAttributes(array(
                  'user_id'=> Yii::app()->user->uid
               ));

Now I want to count the number of rows fetched. Neither $notifyModel->count() work nor count($notifyModel). It is very simple but googling did not help.

Answer

Willem Renzema picture Willem Renzema · Dec 5, 2012
$notifyModels = Notification::model()->findAllByAttributes(array(
            'user_id'=> Yii::app()->user->uid
        ));

$count = count($notifyModels);

Or

$count = Notification::model()->countByAttributes(array(
            'user_id'=> Yii::app()->user->uid
        ));