简体   繁体   English

yii关系总数

[英]yii relation total count

I have two tables 我有两张桌子

first CAT 第一个CAT

id
name

second POST: 第二次发布:

id
name
cat_id

I want to print count posts for each category for example 我想为每个类别打印计数帖子

Sport - 3 posts Culture - 2 posts 体育-3个帖子文化-2个帖子

i want to realize with relations in my model category: 我想通过模型类别中的关系来实现:

'total'=>array(self::STAT, 'Post', 'id')

in my controller i write: 在我的控制器中,我写道:

$model = Category::model()->findAll();
foreach($model as $mod) {
 $model->name.'-('.$model->total.')<br>';
}

Result for each category i receive 我收到的每个类别的结果

Sport - (1) Culture - (1) 运动-(1)文化-(1)

In sport category i have three posts ( 在运动类别中,我有3个帖子(

you can do like this: 您可以这样:

    $model = Category::model()->with('total')-> findAll();

    foreach ($model as $key => $value) {
        echo $value->name.'-('.$value->total.')<br>';
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM