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

时间:2018-04-14 16:15:07

标签: yii2

我有这三个表,如下所示

ManyToMany

课程和教师表在course_instructors中链接在一起

模型:课程

    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);
 } 

然后,

型号: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']);
} 

CourseControllers

    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> 

从我的课程详细信息视图中,我想显示讲师全名

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

而不是课程详细信息视图中的instructor_id

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

这就是

Detail View

问题是如何在课程详细信息视图中显示讲师全名而不是讲师_id,因为它是多对多的。

2 个答案:

答案 0 :(得分:1)

您应该使用框架功能并使用ActiveRecord模型的关系。从

更改您的行
<td><?= $courseInstructor->instructor_id ?></td>

以下

<td><?= $courseInstructor->instructors->first_name.' '.$courseInstructor->instructors->last_name ?></td>

更好的方法是在模型Instructors

中添加以下方法
public function getFullName(){
   return $this->first_name.' '.$this->middle_name.' '.$this->last_name;
}

然后像

一样使用它
<td><?= $courseInstructor->instructors->fullName ?></td>

答案 1 :(得分:0)

这应该在细节视图中工作:
名字<td><?= $courseInstructor->instructors->first_name ?></td>

姓氏为

<td><?= $courseInstructor->instructors->last_name ?></td> 您可以加入名称字符串以创建全名。

这来自

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

让我知道它是否适合你。