php Yii:如何计算模型中的记录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13713590/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Yii : how to count records in a model?
提问by Gunah Gaar
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.
现在我想计算获取的行数。既不$notifyModel->count()工作也不count($notifyModel). 这很简单,但谷歌搜索没有帮助。
回答by Willem Renzema
$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
));
回答by Paul Gladkov
the right usage of count():
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
请参阅http://www.yiiframework.com/doc/api/1.1/CActiveRecord#count-detail
回答by Arfeen
try this:
尝试这个:
$userid = Yii::app()->user->uid;
$notifyModel = Notification::model()->count(
array('condition' => 'user_id=:userid',
'params'=>array(':userid' => $userid)
));
回答by Anil Bhattarai100
$count = Notification::model()->countByAttributes(array(
'user_id'=> Yii::app()->user->uid
));
回答by Always Sunny
I think it is much faster than others
我认为它比其他人快得多
$userTable=User::model()->tableName();
$userid = Yii::app()->user->uid;
$criteria=new CDbCriteria();
$criteria->select=count(id);
$criteria->compare('user_id',$userid);
$count=Yii::app()->db->commandBuilder->createFindCommand($userTable,$criteria)->queryScalar();
回答by Brainfeeder
Since the questions titleis about calling the count function in a modelI'll add some for those beginners reading this :)
由于问题title是关于调用计数函数,in a model我将为那些阅读本文的初学者添加一些:)
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;
}
回答by trai bui
simple ways:
简单的方法:
$model = News::model()->findAll(); //returns AR objects
$count = count($model);
回答by Macedonian
That method is wrong! You trying to get by selecting all rows from database, you load the server, but that's wrong way! All you need to do :
那个方法是错误的!您试图通过从数据库中选择所有行来获取,您加载了服务器,但这是错误的方式!所有你需要做的:
$sql = "SELECT COUNT(*) FROM {{...table_name...}}";
$count = intval(Yii::app()->db
->createCommand($sql)
->queryScalar());
Or you can create function in your model:
或者您可以在模型中创建函数:
Class User extends CActiveRecord
{
private $_total;
public function getTotalItems()
{
if( empty( $this->_total )) {
$this->_total = intval(Yii::app()->db
->createCommand($sql)->queryScalar());
}
return $this->_total;
}
}
then you can use this functions like this:
那么你可以像这样使用这个函数:
$totalItems = User::model()->totalItems;
or :
或者 :
$model = User::model()->findByPk( $uid );
$totalItems = $model->totalItems;
or :
或者 :
$model = new User;
$totalItems = $model->totalItems;
回答by Jehan Wijesinghe
This is the most simple way to do that:
这是最简单的方法:
$count = Table::Model()
->count("field=:field", array("field" => $fildID));
echo $count;
回答by MjM
I strongly recommend you to find count with SQL query else it will degrade your system performance.
There are three way to find out count with the SQL query itself in Yii 1CActiveRecord, those are:
我强烈建议您使用 SQL 查询查找计数,否则它会降低您的系统性能。有三种方法可以通过 SQL 查询本身找出计数Yii 1CActiveRecord,它们是:
1. count() method
1.count() 方法
Finds the number of rows satisfying the specified query condition.
查找满足指定查询条件的行数。
Example :
例子 :
$count = Notification::model()->count('user_id'=> Yii::app()->user->uid);
2. countByAttributes() method (available since v1.1.4)
2. countByAttributes() 方法(自 v1.1.4 可用)
Finds the number of rows that have the specified attribute values.
查找具有指定属性值的行数。
Example :
例子 :
$count = Notification::model()->countByAttributes(array(
'user_id'=> Yii::app()->user->uid
));
3. countBySql() method
3.countBySql() 方法
Finds the number of rows using the given SQL statement. This is equivalent to calling CDbCommand::queryScalarwith the specified SQL statement and the parameters.
使用给定的 SQL 语句查找行数。这相当于CDbCommand::queryScalar使用指定的 SQL 语句和参数调用。
Example:
例子:
$count = Notification::model()
->countBySql("select *from notification where user_id=:userId",
array(':userId'=>Yii::app()->user->uid)
);
Don't use the following code, which will reduce system performance:
$notifyModels = Notification::model()->findAllByAttributes(array( 'user_id'=> Yii::app()->user->uid )); $count = count($notifyModels);Because, there are two function call to find the count
不要使用以下代码,这会降低系统性能:
$notifyModels = Notification::model()->findAllByAttributes(array( 'user_id'=> Yii::app()->user->uid )); $count = count($notifyModels);因为,有两个函数调用来查找计数

