php - Yii2 GridView sorting by count(*) from another table -


my app has these tables:

  • article(include category_id column references category(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

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -