Yii2 GridView 和 DetailView 中如何显示有多条关联记录?
How to show has many relation record in Yii2 GridView and DetailView?
我想在详细视图和网格视图中显示员工有很多爱好 table 爱好。
但是我得到了一个错误异常Trying to get property of non-object
这是我的架构代码模型:
app\model\TblDataStaff
....
public function getTblDataHobis()
{
return $this->hasMany(TblDataHobies::className(), ['id_staff' => 'id']);
}
view code: view.
<?= DetailView::widget([
'model' => $model,
'attributes' => [
...
['attribute'=>'namHob','value'=>$model->tblDataHobis->id],
...
],
]) ?>
index:
<?= GridView::widget([
'dataProvider' => $dataProvider,
//'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
......
['attribute'=>'namHob','value'=>function($namHob){return $namHob->tblDataHobis->name_hobby;},],
.....
['class' => 'yii\grid\ActionColumn'],
],]);?>
如何展现员工的众多爱好?
试试:
<?= DetailView::widget([
'model' => $model,
'attributes' => [
[
'header' => 'number of hobbies',
'value' => function($data) {
return $data->getTblDataHobis()->count();
}
]
]) ?>
没什么奇怪的,你有一个 Trying to get property of non-object
错误只是因为 $model->tblDataHobis
return 一个 TblDataHobies
对象的数组。
你可以试试这个:
// display hobbies names separated with commas
echo implode(', ', \yii\helpers\ArrayHelper::map($model->tblDataHobis, 'id', 'name_hobby'));
对于DetailView
:
'value' => implode(\yii\helpers\ArrayHelper::map($model->tblDataHobis, 'id', 'name_hobby')),
对于GridView
:
'value' => function($model) {
return implode(\yii\helpers\ArrayHelper::map($model->tblDataHobis, 'id', 'name_hobby')),
},
只需获取项目数据,然后创建一个简单的 yii2 查询。
例如我的'worker'有城市id,有了这个id我们可以通过这个id找到城市名称。
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'id',
'name:ntext',
'surname:ntext',
'fathers',
[
'attribute' => 'city_id',
'value'=> function($data){
$city = City::find()->where(['id'=>$data->city_id])->one();
$info = $city->name;
return $info;
},
'format'=>'html',
],
'status',
'street',
'in_company',
],
]) ?>
我想在详细视图和网格视图中显示员工有很多爱好 table 爱好。
但是我得到了一个错误异常Trying to get property of non-object
这是我的架构代码模型:
app\model\TblDataStaff
....
public function getTblDataHobis()
{
return $this->hasMany(TblDataHobies::className(), ['id_staff' => 'id']);
}
view code: view.
<?= DetailView::widget([
'model' => $model,
'attributes' => [
...
['attribute'=>'namHob','value'=>$model->tblDataHobis->id],
...
],
]) ?>
index:
<?= GridView::widget([
'dataProvider' => $dataProvider,
//'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
......
['attribute'=>'namHob','value'=>function($namHob){return $namHob->tblDataHobis->name_hobby;},],
.....
['class' => 'yii\grid\ActionColumn'],
],]);?>
如何展现员工的众多爱好?
试试:
<?= DetailView::widget([
'model' => $model,
'attributes' => [
[
'header' => 'number of hobbies',
'value' => function($data) {
return $data->getTblDataHobis()->count();
}
]
]) ?>
没什么奇怪的,你有一个 Trying to get property of non-object
错误只是因为 $model->tblDataHobis
return 一个 TblDataHobies
对象的数组。
你可以试试这个:
// display hobbies names separated with commas
echo implode(', ', \yii\helpers\ArrayHelper::map($model->tblDataHobis, 'id', 'name_hobby'));
对于DetailView
:
'value' => implode(\yii\helpers\ArrayHelper::map($model->tblDataHobis, 'id', 'name_hobby')),
对于GridView
:
'value' => function($model) {
return implode(\yii\helpers\ArrayHelper::map($model->tblDataHobis, 'id', 'name_hobby')),
},
只需获取项目数据,然后创建一个简单的 yii2 查询。 例如我的'worker'有城市id,有了这个id我们可以通过这个id找到城市名称。
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'id',
'name:ntext',
'surname:ntext',
'fathers',
[
'attribute' => 'city_id',
'value'=> function($data){
$city = City::find()->where(['id'=>$data->city_id])->one();
$info = $city->name;
return $info;
},
'format'=>'html',
],
'status',
'street',
'in_company',
],
]) ?>