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;
}
Author by
Gunah Gaar
Updated on September 17, 2020Comments
-
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 norcount($notifyModel)
. It is very simple but googling did not help.-
crafter almost 9 yearsJust as an aside note, findByAttributes() will return a count of one, always. findAllByAttributes() will return > 1/.
-
-
Gunah Gaar over 11 yearsWhen I put 'findAllByAttributest ' got this error: 'Trying to get property of non-object '
-
Willem Renzema over 11 yearsAdd a line with
Yii::app()->user->uid;
by itself and see if that gives the error or not. By defaultuid
is not defined as a property of the user component, so unless you extended it properly it would give an error. -
Gunah Gaar over 11 yearsuid is working with other things. I have explicitly defined it in UserIdentity class
-
Gunah Gaar over 11 yearswith 'findByAttributes' it returns wrong data and with your solution it generates error!
-
Anil Bhattarai100 about 10 yearsthere is no things to describe all the work to count is done by the yii function countByattributes other things there are all normal
-
Andy over 9 yearsYou shouldn't do this as it loads all models into memory. Use count method as above
-
Shailesh Yadav over 6 years->user->uid, what is the user and uid in that?
-
Shailesh Yadav over 6 yearsI 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 over 6 yearsI 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 over 6 years@ShaileshYadav Please create a new question, rather than making a comment on a 5 year old question.
-
rob006 about 5 yearsFirst example is extremely inefficient, you should avoid using it.