Yii : how to count records in a model?

65,354

Solution 1

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

Solution 2

the right usage of count():


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

Please see http://www.yiiframework.com/doc/api/1.1/CActiveRecord#count-detail

Solution 3

try this:

$userid =  Yii::app()->user->uid;

$notifyModel = Notification::model()->count(
           array('condition' => 'user_id=:userid', 
                 'params'=>array(':userid' => $userid)
                ));

Solution 4

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

Solution 5

Since the questions title is about calling the count function in a model I'll add some for those beginners reading this :)

A function inside a model could look like this:

/**
 * Count the number of rows which match the user ID
 * @param int $uid The user ID
 * @return int The number of found rows
 */
public function getCountByUserID($uid)
{
    $count = $this->count(array(
        'condition'=>'user_id = :uid',
        'params'=>array(
            ':uid'=>$uid,
        ),
    ));
    return $count;
}
Share:
65,354
Gunah Gaar
Author by

Gunah Gaar

Updated on September 17, 2020

Comments

  • Gunah Gaar
    Gunah Gaar over 3 years

    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.

    • crafter
      crafter almost 9 years
      Just as an aside note, findByAttributes() will return a count of one, always. findAllByAttributes() will return > 1/.
  • Gunah Gaar
    Gunah Gaar over 11 years
    When I put 'findAllByAttributest ' got this error: 'Trying to get property of non-object '
  • Willem Renzema
    Willem Renzema over 11 years
    Add a line with Yii::app()->user->uid; by itself and see if that gives the error or not. By default uid is not defined as a property of the user component, so unless you extended it properly it would give an error.
  • Gunah Gaar
    Gunah Gaar over 11 years
    uid is working with other things. I have explicitly defined it in UserIdentity class
  • Gunah Gaar
    Gunah Gaar over 11 years
    with 'findByAttributes' it returns wrong data and with your solution it generates error!
  • Anil Bhattarai100
    Anil Bhattarai100 about 10 years
    there is no things to describe all the work to count is done by the yii function countByattributes other things there are all normal
  • Andy
    Andy over 9 years
    You shouldn't do this as it loads all models into memory. Use count method as above
  • Shailesh Yadav
    Shailesh Yadav over 6 years
    ->user->uid, what is the user and uid in that?
  • Shailesh Yadav
    Shailesh Yadav over 6 years
    I have table companies, I want to print a number of records in that table on my frontend/site/home.php, how can I do this?
  • Shailesh Yadav
    Shailesh Yadav over 6 years
    I am new in Yii framework, I have table companies, I want to print a number of records in that table on my frontend/site/home.php, how can I do this? (DB name-yii2advanced)
  • Willem Renzema
    Willem Renzema over 6 years
    @ShaileshYadav Please create a new question, rather than making a comment on a 5 year old question.
  • rob006
    rob006 about 5 years
    First example is extremely inefficient, you should avoid using it.