I have 3 tables and i have made relation to What table by using Field table. i want to show name row from What table in Gridview.but it shows me row of data seperated by comma. i want each data in different column. like sql query result with repeated field.
this is my tbon.php model:
public function getWhat()
{
return $this->hasMany(What::className(), ['idw' => 'idw'])
->viaTable('Field', ['id' => 'id']);
}
public function getField()
{
return $this->hasMany(Field::className(), ['id' => 'id']);
}
and in TbonSearch.php i have added these codes:
public $what;
public $field;
and
[['field','what'],'safe']
and in search method, i have added:
$query = Tbon::find()
->joinWith(['field'])
->joinWith(['what']);
and for sorting:
$dataProvider->sort->attributes['what'] = [
'asc' => ['what.name' => SORT_ASC],
'desc' => ['what.name' => SORT_DESC],
];
and add search filter:
->andFilterWhere(['like', 'what.name', $this->what]);
in index i have added this code and here is the problem, i think:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
[
'label'=>'Name',
'attribute' => 'what',
'value' => function ($data) {
$str = '';
foreach($data->what as $name) {
$str .= $name->name.',';
}
return $str;
},
],
data is like this: a,b,c,d for field of name! but i want ID and a then in next row ID and b and etc.
any help will be appreciated!
I dont know your code and I dont get the meaning, but I can answer in general.
A GridView is used to view Data in a Grid. A row in a GridView represents a dataset and presenting data in the form of a table.
foreach($data->what as $name) {
$str .= $name->name.',';
}
I assume you want to display the Name attribute from the relation what. You can simply achieve this with the tablename.columnname syntax.
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
[
'label'=>'Name',
'attribute' => 'what.name',
],
If you want to display the ID column additionally:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
[
'label'=>'Name',
'attribute' => 'what',
'value' => function ($data) {
$str = '';
foreach($data->what as $name) {
$str .= ' ';
$str .= $name->id.'';
$str .= $name->name.',';
}
return $str;
},
],
To be more precise (in Terms of your Problem). We have a datamodel with a junction table like the following simple book database.
Example:
If you want to display the names of the authors on the standard CRUD generated index Gridview, you can do something like this:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'book_name',
[
'label'=>'Author(s)',
'value' => function ($dataProvider) {
$str = '';
foreach($dataProvider->authors as $name) {
$str .= ' ';
$str .= $name->id.' ';
$str .= $name->author_name.',';
}
return $str;
},
],
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
with the result:
In addition, you can edit the Detail View to display such an information in an extra Gridview. The result can look like this.
To achieve this you have to:
view.php aka the view
use yii\grid\GridView;
// other code
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
'id',
[
'attribute' => 'author.author_name',
'header' => 'Author(s)',
],
],
]); ?>
BookController.php aka the controller
public function actionView($id)
{
$dataProvider = Book::getAllAuthors($id);
return $this->render('view', [
'model' => $this->findModel($id),
'dataProvider' => $dataProvider,
]);
}
Book.php aka the model
use \yii\data\ActiveDataProvider;
// other code
public static function getAllAuthors($id) {
$query = BookAuthor::find()->where(['book_id' => $id]);
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
return $dataProvider;
}