I have these tables as shown below
7647
course model
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']);
}
instructor model
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);
}
course_instructors model
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']);
}
Course Controller
public function actionView($id)
{
$model = $this->findModel($id);
$courseinstructors = $model->courseInstructors;
return $this->render('view', [
'model' => $model,
'courseinstructors' => $courseinstructors,
]);
}
Course: Detail View
<?= 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>
From my Course Detail view, I want to display the instructor fullname
function getInstructorFullName()
{
return ($this->first_name." ".$this->middle_name." ".$this->last_name);
}
instead of the instructor_id in
<td><?= $courseInstructor->instructor_id ?></td>
This is what am getting
7648
How do I display the instructor full name in the course detail view instead of the instructor_id, since it is many to many.
Thanks