【发布时间】:2024-05-29 15:00:02
【问题描述】:
我想在详细视图和网格视图中展示员工的许多爱好。
但是我遇到了错误异常Trying to get property of non-object
这是我的架构代码模型:
应用\模型\TblDataStaff
....
public function getTblDataHobis()
{
return $this->hasMany(TblDataHobies::className(), ['id_staff' => 'id']);
}
查看代码:查看。
<?= DetailView::widget([
'model' => $model,
'attributes' => [
...
['attribute'=>'namHob','value'=>$model->tblDataHobis->id],
...
],
]) ?>
索引:
<?= 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'],
],]);?>
如何展示员工的众多爱好?
【问题讨论】:
标签: php gridview yii2 detailview