I have GridView with ActionColumn where i make custom urls. And i cant seem to get values from relations.
Has anyone done something like that or has some other suggestion with wich i can create urls in GridView useing relation data
Current code throws "Trying to get property of non-object" error on line
return Yii::$app->getUrlManager()->createUrl(['site/download', 'id' => $model->downloads->key])
Controller
//...
$model = new MailmasterFiles();
//...
Model
//...
public function getDownloads() {
return $this->hasOne(Download::className(), ['id' => 'download_id']);
}
//...
public function search($params, $options = [], $pagnation = 10) {
$query = MailmasterFiles::find();
if (isset($options['join']))
$query->joinWith($options['join']);
$this->setScenario('search');
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => [
'pageSize' => $pagnation,
],
]);
if (isset($options['sort']))
$dataProvider->setSort($options['sort']);
if (!($this->load($params))) {
return $dataProvider;
}
$query->andFilterWhere(['like', 'from', $this->from])
->andFilterWhere(['like', 'till', $this->till])
->andFilterWhere(['like', 'skip', $this->skip])
->andFilterWhere(['like', 'created', $this->created]);
return $dataProvider;
}
//...
View
//...
echo GridView::widget([
'dataProvider' => $model->search(Yii::$app->request->get(), $option = [
'sort' => [
'defaultOrder' => [
'created' => SORT_DESC,
]
],
'join' => ['downloads'],
]),
'columns' => [
[
'class' => 'kartik\grid\ActionColumn',
'template' => '{download}',
'header' => Yii::t('app', 'Download'),
'buttons' => [
'download' => function ($url, $model, $key) {
$html = '<span class="glyphicon glyphicon-file"></span>';
$html = Html::a($html, $url, ['title' => Yii::t('app', 'Download')]);
return $html;
}
],
'urlCreator' => function ($action, $model, $key, $index) {
//Problem with this line
return Yii::$app->getUrlManager()->createUrl(['site/download', 'id' => $model->downloads->key]);
}
],
],
]);
//...