15

モデルからデータをフェッチするための次のコードがあります。

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

ここで、フェッチされた行の数を数えたいと思います。仕事$notifyModel->count()count($notifyModel)。とてもシンプルですが、グーグルは役に立ちませんでした。

4

11 に答える 11

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

$count = count($notifyModels);

または

$count = Notification::model()->countByAttributes(array(
            'user_id'=> Yii::app()->user->uid
        ));
于 2012-12-04T23:10:49.630 に答える
30

count()の正しい使用法:


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

http://www.yiiframework.com/doc/api/1.1/CActiveRecord#count-detailを参照してください

于 2013-02-21T19:26:31.380 に答える
4

これを試して:

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

$notifyModel = Notification::model()->count(
           array('condition' => 'user_id=:userid', 
                 'params'=>array(':userid' => $userid)
                ));
于 2012-12-05T10:58:51.993 に答える
3
$count = Notification::model()->countByAttributes(array(
    'user_id'=> Yii::app()->user->uid
));
于 2014-02-12T11:11:30.763 に答える
2

質問titleはカウント関数の呼び出しに関するものなので、in a modelこれを読んでいる初心者のためにいくつか追加します:)

モデル内の関数は次のようになります。

/**
 * 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;
}
于 2013-06-18T09:46:41.297 に答える
2

簡単な方法:

$model = News::model()->findAll(); //returns AR objects
         $count = count($model);
于 2014-01-08T03:35:11.267 に答える
2

他の人よりずっと速いと思います

$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();
于 2014-06-03T16:31:06.083 に答える
1

これは、これを行うための最も簡単な方法です。

$count = Table::Model()
         ->count("field=:field", array("field" => $fildID));
echo $count;
于 2014-04-29T06:36:28.910 に答える
1

その方法は間違っています!データベースからすべての行を選択して取得しようとすると、サーバーがロードされますが、それは間違った方法です。あなたがする必要があるすべて:

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

}

次に、次のようにこの関数を使用できます。

$totalItems = User::model()->totalItems;

また :

$model = User::model()->findByPk( $uid );
$totalItems = $model->totalItems;

また :

$model = new User;
$totalItems = $model->totalItems;
于 2014-08-08T21:54:37.463 に答える
0

SQLクエリでカウントを見つけることを強くお勧めします。そうしないと、システムパフォーマンスが低下します。SQLクエリ自体でカウントを確認する方法は3つありますYii 1 CActiveRecord。それらは次のとおりです。

1. count()メソッド

指定されたクエリ条件を満たす行数を検索します。

例 :

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

2. countByAttributes()メソッド(v1.1.4以降で使用可能)

指定された属性値を持つ行の数を検索します。

例 :

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

3. countBySql()メソッド

指定されたSQLステートメントを使用して行数を検索します。これはCDbCommand::queryScalar、指定されたSQLステートメントとパラメーターを使用して呼び出すのと同じです。

例:

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

なぜなら、カウントを見つけるための2つの関数呼び出しがあるからです

于 2019-09-12T09:31:51.767 に答える
-1

私の研究では、最も簡単でベストプラクティスは次のようなものです。

$notifyModel = Notification::model()->count(array('user_id'=> Yii::app()->user->uid));
于 2019-04-09T07:26:46.123 に答える