Yii2 Yii-在DetailView中显示多对多属性

Yii2 Yii-在DetailView中显示多对多属性,yii2,Yii2,我有这三张表,如下所示 课程和讲师表在课程讲师中链接在一起 模式:课程 public function attributeLabels() { return [ 'id' => Yii::t('course', 'Course ID'), 'course_name' => Yii::t('course', 'Course Name'), 'course_code' => Yii::t('course', 'Cou

我有这三张表,如下所示

课程和讲师表在课程讲师中链接在一起

模式:课程

    public function attributeLabels()
{
    return [
        'id' => Yii::t('course', 'Course ID'),
        'course_name' => Yii::t('course', 'Course Name'),
        'course_code' => Yii::t('course', 'Course Code'),
    ];
}

public function getCourseInstructors()
{
    return $this->hasMany(CourseInstructors::className(), ['course_id' => 'id']);
}  
而且

型号:讲师

    public function attributeLabels()
{
    return [
        'instructor_id' => Yii::t('ins', 'Instructor ID'),
        'first_name' => Yii::t('ins', 'First Name'),
        'middle_name' => Yii::t('ins', 'Middle Name'),
        'last_name' => Yii::t('ins', 'Last Name'),            
    ];
}

 function getInstructorFullName()
 {
    return ($this->first_name." ".$this->middle_name." ".$this->last_name);
 } 
那么

模型:课程讲师

    public function attributeLabels()
{
    return [
        'id' => 'Course Instructor ID',
        'course_id' => 'Course',
        'instructor_id' => 'Course Instructor',
        'remark' => 'Remark',
    ];
}

public function getCourses()
{
    return $this->hasOne(Courses::className(), ['id' => 'course_id']);
} 

public function getInstructors()
{
    return $this->hasOne(Instructors::className(), ['instructor_id' => 'instructor_id']);
} 
    public function actionView($id)
{

        $model = $this->findModel($id);
        $courseinstructors = $model->courseInstructors;

        return $this->render('view', [
            'model' => $model,
            'courseinstructors' => $courseinstructors,
        ]);
    }
课程控制员

    public function attributeLabels()
{
    return [
        'id' => 'Course Instructor ID',
        'course_id' => 'Course',
        'instructor_id' => 'Course Instructor',
        'remark' => 'Remark',
    ];
}

public function getCourses()
{
    return $this->hasOne(Courses::className(), ['id' => 'course_id']);
} 

public function getInstructors()
{
    return $this->hasOne(Instructors::className(), ['instructor_id' => 'instructor_id']);
} 
    public function actionView($id)
{

        $model = $this->findModel($id);
        $courseinstructors = $model->courseInstructors;

        return $this->render('view', [
            'model' => $model,
            'courseinstructors' => $courseinstructors,
        ]);
    }
详细视图:课程

    <?= DetailView::widget([
    'model' => $model,
    'options'=>['class'=>'table  detail-view'],
    'attributes' => [
        'course_name',
        'course_code',
    ],
]) ?>

<h2>Details</h2>
<table class="receipt-details table">
    <tr>
        <th>ID</th>
        <th>Instructor Name</th>
        <th>Remark</th>
    </tr>
    <?php foreach($model->courseInstructors as $courseInstructor) :?>
        <tr>
            <td><?= $courseInstructor->id ?></td>
            <td><?= $courseInstructor->instructor_id ?></td>
            <td><?= $courseInstructor->remark ?></td>
        </tr>
    <?php endforeach; ?>
</table> 
而不是课程详细信息视图中的讲师id

<td><?= $courseInstructor->instructor_id ?></td

这应该在细节视图中起作用:
表示名字

姓氏的

您可以加入名称字符串以生成全名

这来自

public function getInstructors()
{
    return $this->hasOne(Instructors::className(), ['instructor_id' => 'instructor_id']);
}

让我知道它是否适合您。

您应该使用框架电源并使用
ActiveRecord
模型的关系。将您的线路从

<td><?= $courseInstructor->instructor_id ?></td>
然后像这样使用它

<td><?= $courseInstructor->instructors->fullName ?></td>

<td><?= $courseInstructor->instructors->fullName ?></td>