Php Yii:如何计算模型中的记录?

Php Yii:如何计算模型中的记录?,php,yii,Php,Yii,我有以下代码从模型中获取数据 $notifyModel = Notification::model()->findByAttributes(array( 'user_id'=> Yii::app()->user->uid )); 现在我想计算提取的行数。 $notifyModel->count()工作和计数($notifyModel)。 这很简单,但谷歌搜索没有帮助 $notifyModels = Not

我有以下代码从模型中获取数据

$notifyModel = Notification::model()->findByAttributes(array(
                  'user_id'=> Yii::app()->user->uid
               ));
现在我想计算提取的行数。
$notifyModel->count()
工作和
计数($notifyModel)
。 这很简单,但谷歌搜索没有帮助

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

$count = count($notifyModels);

试试这个:

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

$notifyModel = Notification::model()->count(
           array('condition' => 'user_id=:userid', 
                 'params'=>array(':userid' => $userid)
                ));
count()的正确用法:


请参见

,因为问题
标题
是关于在模型中调用计数函数
我将为阅读本文的初学者添加一些:)

模型中的函数可能如下所示:

/**
 * 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;
}
$totalItems = User::model()->totalItems;
简单方法:

$model = News::model()->findAll(); //returns AR objects
         $count = count($model);
$count=Notification::model()->countByAttributes(数组(
'user_id'=>Yii::app()->user->uid
));

这是最简单的方法:

$count = Table::Model()
         ->count("field=:field", array("field" => $fildID));
echo $count;

我认为它比其他的要快得多

$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();
这种方法是错误的! 您试图通过从数据库中选择所有行来获取,您加载了服务器,但这是错误的方法! 您需要做的是:

$sql = "SELECT COUNT(*) FROM {{...table_name...}}";

$count = intval(Yii::app()->db
  ->createCommand($sql)
  ->queryScalar());
也可以在模型中创建函数:

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;
    }

}
然后您可以像这样使用这些函数:

/**
 * 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;
}
$totalItems = User::model()->totalItems;
或:

或:


在我的研究中,最简单和最好的实践如下

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

我强烈建议您使用SQL查询查找计数,否则会降低系统性能。 在
yii1
CActiveRecord
中,有三种方法可以通过SQL查询本身找到计数,它们是:

1。count()方法

查找满足指定查询条件的行数

例如:

2。countByAttributes()方法(从1.1.4版开始提供)

查找具有指定属性值的行数

例如:

3。countBySql()方法

使用给定的SQL语句查找行数。这相当于使用指定的SQL语句和参数调用
CDbCommand::queryScalar

例如:

不要使用以下代码,这将降低系统性能:

$notifyModels = Notification::model()->findAllByAttributes(array(
            'user_id'=> Yii::app()->user->uid
        ));
$count = count($notifyModels);
因为,有两个函数调用来查找计数


当我放置'findAllByAttributest'时,出现了以下错误:“尝试获取非对象的属性”添加一行
Yii::app()->user->uid并查看是否出现错误。默认情况下,
uid
未定义为用户组件的属性,因此除非您正确扩展它,否则会出现错误。uid正在处理其他内容。我已经在UserIdentity类中使用“FindByatAttributes”明确定义了它,它返回错误的数据,并且使用您的解决方案生成错误!->用户->uid,其中的用户和uid是什么?没有任何东西可以描述所有要计数的工作都是由yii函数countByattributes完成的其他东西都是正常的,您不应该这样做,因为它会将所有模型加载到内存中。如上所述使用count方法正如旁注一样,findByAttributes()将始终返回一个计数。findAllByAttributes()将返回>1/。我是Yii框架的新手,我有表格公司,我想在我的frontend/site/home.php上打印该表格中的许多记录,我该怎么做?(DB名称-YII2高级)
$notifyModel = Notification::model()->count(array('user_id'=> Yii::app()->user->uid));
$count = Notification::model()->count('user_id'=> Yii::app()->user->uid);
$count = Notification::model()->countByAttributes(array(
    'user_id'=> Yii::app()->user->uid
));
$count = Notification::model()
         ->countBySql("select *from notification where user_id=:userId",
            array(':userId'=>Yii::app()->user->uid)
           );
$notifyModels = Notification::model()->findAllByAttributes(array(
            'user_id'=> Yii::app()->user->uid
        ));
$count = count($notifyModels);