1

please help I dont know how to get relation table when using sqldataprovider. Anyone understand how to use relation model?

$model = new Finalresult();
$searchModel = new FinalresultSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);

$dataProvider = new SqlDataProvider([
   'sql' => 'SELECT finalresult.bib,
            finalresult.series_id,
            finalresult.category_id,
            GROUP_CONCAT(DISTINCT finalresult.point ORDER BY series.serie_seri_no DESC) AS seriPoint 
            FROM finalresult, series GROUP BY finalresult.bib',
    'key' => 'bib',
]);

I'm trying to get relation table:

'attribute'=>'category_id', 
'width'=>'300px',
'value'=>function ($model, $key, $index, $widget) { 
     return $model->category->category_name;
},

then getting trying to non-object

2

2 Answers 2

2

You can't use relations with SqlDataProvider, because each single result will be presented as array, for example:

[
    'id' => 1,
    'name' => 'Some name',
    'category_id' => 1,
],

For example, you can access category_id as `$model['category_id'].

SqlDataProvider is for very very complex queries, your query can easily be written as ActiveQuery and you can use ActiveDataProvider and get all advantages of that (relations, etc.).

You can find category by id, but it will be lazily loaded that means amount of queries is multiplied by number of rows.

With ActiveDataProvider and relations you can use eager loading and reduce amount of queries. Read more in official docs.

Sign up to request clarification or add additional context in comments.

Comments

0

Grid Columns example in documentation

try to change "value" to

'value'=> function($data) { 
     return $data['category']['category_name'];
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.