Reputation: 217
I have these three tables as shown below
courses and instructors tables are linked together in course_instructors
Model: Course
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']);
}
and also
Model:Instructor
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);
}
Then,
Model: 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,
]);
}
Detail View: course
<?= 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 the Course Detail View
<td><?= $courseInstructor->instructor_id ?></td
This is what am getting
The question is how do I display the instructor full name in the course detail view instead of the instructor_id, since it is many to many.
Upvotes: 1
Views: 353
Reputation: 23778
You should use the framework power and consume the relations for the ActiveRecord
models. Change your line from
<td><?= $courseInstructor->instructor_id ?></td>
to the following
<td><?= $courseInstructor->instructors->first_name.' '.$courseInstructor->instructors->last_name ?></td>
a better way would be to add the following method inside the model Instructors
public function getFullName(){
return $this->first_name.' '.$this->middle_name.' '.$this->last_name;
}
and then use it like
<td><?= $courseInstructor->instructors->fullName ?></td>
Upvotes: 1
Reputation: 2302
This should work in the Detail View:
<td><?= $courseInstructor->instructors->first_name ?></td>
for first name.
<td><?= $courseInstructor->instructors->last_name ?></td>
for last name.
You can join the name string to make a full name.
This comes from
public function getInstructors()
{
return $this->hasOne(Instructors::className(), ['instructor_id' => 'instructor_id']);
}
Let me know if it works for you.
Upvotes: 0