noorr_swati
(Nooralamswati91)
January 12, 2015, 10:01am
1
i have a relation in a model, in index it is displaying the id i want to show the name based on that foreign key record, in Yii.1 we just write relationName.Attribute and it display the value we want but how to do it with yii 2
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'fld_id',
'fld_width',
'fld_height',
'fld_material_id',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
in place of fld_material_id i want the name of that material in material table
amit5992
(Gupta5992amit)
January 12, 2015, 11:19am
2
noorr_swati:
i have a relation in a model, in index it is displaying the id i want to show the name based on that foreign key record, in Yii.1 we just write relationName.Attribute and it display the value we want but how to do it with yii 2
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'fld_id',
'fld_width',
'fld_height',
'fld_material_id',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
in place of fld_material_id i want the name of that material in material table
First create relation in model file
public function getRelmaterial()
{
return $this->hasOne(model_name::className(), ['fld_material_id' => 'material_id']);
}
second, in modelSearch file
add this code
$query->joinWith(['relmaterial'']);
at last ,
use this relation like…
[
'label' => 'Material Name',
'attribute' => 'fld_material_id',
'value' => [size=2] '[/size][size=2]relmaterial.[/size][size=2]material_name',[/size]
],
noorr_swati
(Nooralamswati91)
January 12, 2015, 11:54am
3
Amit5992:
First create relation in model file
public function getRelmaterial()
{
return $this->hasOne(model_name::className(), ['fld_material_id' => 'material_id']);
}
second, in modelSearch file
add this code
$query->joinWith(['relmaterial'']);
at last ,
use this relation like…
[
'label' => 'Material Name',
'attribute' => 'fld_material_id',
'value' => [size=2] '[/size][size=2]relmaterial.[/size][size=2]material_name',[/size]
],
thanks i have another question if i want to show two attirbutes values in place of one like i want to show fld_name and fld_lastnmae what should i have to do
amit5992
(Gupta5992amit)
January 12, 2015, 12:33pm
4
you can use this…
[code]
[
‘attribute’ => ‘Name’,
‘value’ => function ($data) {
return $data->first_name." ".$data->last_name;
},],
noorr_swati
(Nooralamswati91)
January 12, 2015, 12:50pm
5
Amit5992:
you can use this…
[
'attribute' => 'Name',
'value' => function ($data) {
return $data->first_name." ".$data->last_name;
},],
great but i need the data of the other model its considering it in the current model and giving the error Getting unknown property: app\models\Series::fld_width
amit5992
(Gupta5992amit)
January 12, 2015, 1:52pm
6
define new object or render new object of another model at top of gridview or controller file
like ,
if $demo is another model object, so use it like
[ 'attribute' => 'Name', 'value' => function ($demo) {
return $demo->first_name." ".$demo->last_name;
},],
noorr_swati
(Nooralamswati91)
January 12, 2015, 1:59pm
7
Amit5992:
define new object or render new object of another model at top of gridview or controller file
like ,
if $demo is another model object, so use it like
[ 'attribute' => 'Name', 'value' => function ($demo) {
return $demo->first_name." ".$demo->last_name;
},],
i created the object before cgridView like this
$data = new Size();
but still its not working
amit5992
(Gupta5992amit)
January 12, 2015, 2:10pm
8
can you give demo code for this error
noorr_swati
(Nooralamswati91)
January 12, 2015, 2:11pm
9
Here it is Controller
public function actionIndex()
{
$searchModel = new Series_Search();
$size = new Size();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'size' => $size,
'dataProvider' => $dataProvider,
]);
}
View
<?=
GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'size' => $size,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'fld_id',
'fld_name',
[
'label' => 'Material Name',
'attribute' => 'fld_material_id',
'value' => 'fldMaterial.fld_name',
],
[
'label' => 'Size',
'attribute' => 'fld_size_id',
// 'value' => 'fldSize.fld_width',
'value' => function ($size) {
return $size->fld_width."x".$size->fld_height;
},
],
// 'fld_size_id',
],
]); ?>
i need fld_width and fld_height from size
the error is
amit5992
(Gupta5992amit)
January 12, 2015, 2:13pm
10
noorr_swati:
Here it is Controller
public function actionIndex()
{
$searchModel = new Series_Search();
$size = new Size();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'size' => $size,
'dataProvider' => $dataProvider,
]);
}
View
<?=
GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'size' => $size,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'fld_id',
'fld_name',
[
'label' => 'Material Name',
'attribute' => 'fld_material_id',
'value' => 'fldMaterial.fld_name',
],
[
'label' => 'Size',
'attribute' => 'fld_size_id',
// 'value' => 'fldSize.fld_width',
'value' => function ($size) {
return $size->fld_width."x".$size->fld_height;
},
],
// 'fld_size_id',
],
]); ?>
i need fld_width and fld_height from size
remove $size from after $searchModel in view file
noorr_swati
(Nooralamswati91)
January 12, 2015, 2:27pm
11
i did that now giving the error
amit5992
(Gupta5992amit)
January 13, 2015, 3:56am
12
use relation for getting another model value…
noorr_swati
(Nooralamswati91)
January 13, 2015, 6:26am
13
how can i use relation i tried on it but its not working
amit5992
(Gupta5992amit)
January 13, 2015, 6:46am
14
create relation that i describe already at post #2 ,follow that step and created it.
noorr_swati
(Nooralamswati91)
January 13, 2015, 7:53am
15
is this code included in yii code???
'value' => [size=2] '[/size][size=2]relmaterial.[/size][size=2]material_name',[/size]
i thought size is written by mistake but its still giving error
amit5992
(Gupta5992amit)
January 13, 2015, 8:03am
16
noorr_swati:
is this code included in yii code???
'value' => [size=2] '[/size][size=2]relmaterial.[/size][size=2]material_name',[/size]
i thought size is written by mistake but its still giving error
can you describe above code … in detail
noorr_swati
(Nooralamswati91)
January 13, 2015, 9:37am
17
this is from your second post above i am asking is this a valid code or there is some typing mistake
amit5992
(Gupta5992amit)
January 13, 2015, 10:08am
18
i rewrite my previous code
e.g : city table
in model create relation like for getting state_name, state_code using state_id;
public function getRelState()
{
return $this->hasOne(State::className(), ['state_id' => 'city_state_id']); //city_state_id is field of city
}
than, in modelSearch file add following code after $query = City::find();
$query->joinWith(['relState']);
last use it in index vews file…:
[
'label' => 'State',
'attribute' => 'state_id',
'value' => 'relState.state_name',
OR
'value' => function ($model) // $model is object of City
{
return $model->relState->state_name." ( ".$model->relState->state_code." ) ";
},
],
noorr_swati
(Nooralamswati91)
January 13, 2015, 10:20am
19
Amit5992:
i rewrite my previous code
e.g : city table
in model create relation like for getting state_name, state_code using state_id;
public function getRelState()
{
return $this->hasOne(State::className(), ['state_id' => 'city_state_id']); //city_state_id is field of city
}
than, in modelSearch file add following code after $query = City::find();
$query->joinWith(['relState']);
last use it in index vews file…:
[
'label' => 'State',
'attribute' => 'state_id',
'value' => 'relState.state_name',
OR
'value' => function ($model) // $model is object of City
{
return $model->relState->state_name." ( ".$model->relState->state_code." ) ";
},
],
yes i was missing the relation name its done thanks alot