1

我有一个使用 yii2 框架的应用程序。

我的应用程序有一个包含表的数据库:

  1. class=>

    • id => 作为PK
    • 班级名称
    • 教师编号
  2. student=>

    • ID
    • class_id => as class_idclass作为表中的外键
    • 学生姓名
    • 年龄
    • 性别
  3. ETC

在我的 index.php( class ) 中,我使用 Kartik Gridview 来显示类的数据。如您所知,gridview 中有很多动作,例如查看按钮动作。示例:我的index.php以 gridView 形式显示来自classwith id= 101 的数据。

如何显示所有= 101student_namestudent表?class_id

我有这个代码:

在我的 class.php 在我的类模型中

public function getStudents() {
    return $this->hasMany(students::className(), ['class_id' => 'class_id']);
}

和我的 gridView 的代码

view.php 在我的班级视图中

<?=
DetailView::widget([
    'model' => $model,
    'condensed' => true,
    'hover' => true,
    'enableEditMode' => false,
    'mode' => DetailView::MODE_VIEW,
    'panel' => [
        'heading' => 'Data Detail',
        'type' => DetailView::TYPE_INFO,
    ],
    'attributes' => [
        'alamat_lengkap',
        'jumlah_dpp',
        'jumlah_ppn',
        'jumlah_ppnbm',
        'fg_uang_muka',
        'uang_muka_dpp',
        'uang_muka_ppn',
        'uang_muka_ppnbm',
        [
            'label' => 'Kode Objek',
            'value' => $model->students->student_name,     //this code didn't work and return error as "Trying to get property of non-object"
        ],
    ],
])
?>

任何帮助都会得到帮助:),谢谢:)

4

1 回答 1

2

对于DetailView

'value' => implode(',', \yii\helpers\ArrayHelper::map($model->fakturOutDetails, 'id', 'student_name')),

对于GridView

'value' => function($model) {
    return implode(',', \yii\helpers\ArrayHelper::map($model->fakturOutDetails, 'id', 'student_name')),
},
于 2017-02-02T07:15:16.943 回答