php - Yii2 GridView sorting by count(*) from another table -
my app has these tables:
- article(include
category_id
column referencescategory(id)
) - category
it outputs gridview:
<?= gridview::widget([ ... 'columns' => [ ... [ 'label' => 'article count', 'attribute' => 'articles', 'value' => function ($model, $key, $index, $column) { return count($model->articles); }, ], ... ], ]) ?>
i want make sorting articles count. how can this?
first of all, dont count value directly in view, model who's admin u r sorting
for use criteria => $criteria = new cdbcriteria();
provide count var in model csort()
$sort = new csort(); $sort->defaultorder = 'id desc'; $sort->attributes = array( 'articles'=>array( 'asc'=>'c.articles asc', 'desc'=>'c.articles desc', ), '*', );
then provide $sort variable activedataprovider
return new cactivedataprovider($this, array( 'criteria'=>$criteria, 'sort' => $sort, ));
Comments
Post a Comment