简体   繁体   中英

yii relation total count

I have two tables

first 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

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)

In sport category i have three posts (

you can do like this:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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