1
votes

I've an application using yii2 framework.

My application has a db that contain tables:

  1. class =>

    • id => as PK
    • class_name
    • teacher_id
  2. student =>

    • id
    • class_id => as class_id as foreign key from class table
    • student_name
    • age
    • sex
  3. Etc

and in my index.php(class), I've use Kartik Gridview for showing the data of the class. As you know, there are many actions in gridview, such as view button action. Example: My index.php show a data from class with id = 101 in gridView form.

How do I can show all student_name from student table that has class_id = 101?

and I've this code:

in my class.php in my class model

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

and code for my gridView

view.php in my class view

<?=
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"
        ],
    ],
])
?>

Any help will be appriciated :), thanks :)

1
update you question and add the code where you invoke the view.php (I think it's index.php) and the controller/action for view.phpScaisEdge

1 Answers

2
votes

For DetailView :

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

For GridView :

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