Hello,
I am trying to allow user to export data grid to xls.
I got two tables - submission and product.
My model looks like this:
class Submission extends \yii\db\ActiveRecord
{
public $name;
public $lang;
public function attributes()
{
// add related fields to searchable attributes
return array_merge(parent::attributes(), ['name', 'lang']);
}
public function getProduct()
{
return $this->hasOne(Product::className(), ['product_pk' => 'product_pk']);
}
public function search($params)
{
$query = Submission::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$query->joinWith(['product']);
if (!($this->load($params) && $this->validate())) {
return $dataProvider;
}
$dataProvider->sort->attributes['name'] = [
'asc' => ['product.name' => SORT_ASC],
'desc' => ['product.name' => SORT_DESC],
];
$dataProvider->sort->attributes['lang'] = [
'asc' => ['product.lang' => SORT_ASC],
'desc' => ['product.lang' => SORT_DESC],
];
$query->andFilterWhere([
'submission_pk' => $this->submission_pk,
'product_pk' => $this->product_pk,
'is_winner' => $this->is_winner,
]);
$query->andFilterWhere(['like', 'email', $this->email])
->andFilterWhere(['like', 'user_name', $this->user_name])
->andFilterWhere(['like', 'product.name', $this->getAttribute('product.name')])
->andFilterWhere(['like', 'product.lang', $this->getAttribute('product.lang')])
;
return $dataProvider;
}
}
My Data grid widget is defined like this:
$searchModel = new Submission();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'submission_pk',
'email:email',
'user_name',
['attribute' => 'name', 'value' => 'product.name'],
['attribute' => 'lang', 'value' => 'product.lang'],
['class' => 'yii\grid\ActionColumn'],
],
]);
and it works pretty well.
For xls export i found this extension: arturoliveira/yii2-excelview
My excelview is defined like this:
$searchModel = new Submission();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
ExcelView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'fullExportType'=> 'xlsx',
'grid_mode' => 'export',
'columns' => [
'submission_pk',
'email',
'user_name',
['attribute' => 'name', 'value' => 'product.name'],
['attribute' => 'lang', 'value' => 'product.lang'],
],
]);
Which is pretty similiar to GridView but when I use export action it exports data with empty values in columns product.name and product.lang.
Any suggestions how to export relation data? I believe i just can`t see some small mistake, but I am open to totaly different approach.
Thanks in advance. (and sorry for my poor english)