has many relation in gridview

i am working on a content - tag(odered) system using yii2.

I have a Content(PK:id) table, I have a Tag(PK:id) table, and I have a junction table called Content_Keyword(PK:content_id, keyword_id,order).

now i want to show all content with same keywords and orders in a grid view (sortable, searchable just as content index page) on keyword view page.

i’m using via in keywords Class according to the documentation as following:


     /**

     * @return \yii\db\ActiveQuery

     */

    public function getContent() 

    {

        return $this->hasMany(Content_Tag::className(), ['content_id' => 'id']);

    }


    /**

     * @return \yii\db\ActiveQuery

     */

    public function getContents()

    {

        return $this->hasMany(Tag::className(), ['tag_id' => 'tag_id'])->viaTable('content_tag', ['content_id' => 'id']);

    }

and modify the kewordscontroller in action view


    {

        $searchModel = new ContentSearch();

        $params = Yii::$app->request->queryParams;

        $params['keyword_id'] = $id;

        $dataProvider = $searchModel->search($params);


        return $this->render('view', [

            'model' => $this->findModel($id),

            'searchModel' => $searchModel,

            'dataProvider' => $dataProvider,

        ]);

    }

And modify the ContentSearch Model As following:


        if(isset($params['keyword_id']))

        {

            $query = KeywordRecord::findOne($params['keyword_id'])->getContents();

        }else{

            $query = ContentRecord::find();

        }

    ...

the keywords/view.php file as following


    <?= DetailView::widget([

        'model' => $model,

        'attributes' => [

            'id',

            'name',

            'level',

            ],

        ],

    ]) ?> 


    <?= GridView::widget([

        'dataProvider' => $dataProvider,

        'filterModel' => $searchModel,

        'columns' => [

            ['class' => 'yii\grid\SerialColumn'],

            'type_id',

            'content'

    ]); ?>

Now the gridview works, sorting, filtering works, but how to show the order in a DataColumn?

I tried CSqlDataProvider and succeed to print order but make the sorting and filtering failed.


    $sql="SELECT content_keyword.order, content.*

              FROM content, content_keyword

              WHERE content.id = content_keyword.order.content_id

              AND content_keyword.keyword_id=$id

              ORDER BY content_keyword.order";

Is there any elegent way to use the ActiveRecord?

i’ve tried add a new attribute ‘contentKeywords.order’ based on has-many relation in ContentSearch Model:


    public function attributes()

    {

        // add related fields to searchable attributes

        return array_merge(parent::attributes(), ['contentKeywords.order']);

    }

    public function search($params)

    ...

        $query->joinWith(['contentKeywords' => function($query) { $query->from(['contentKeywords' => 'content_keword']); }]);

    ...

with no error but failed to display the order( not set) in the grid.