我想与另一个模型中的一个模型相关联,但是出现了这个错误:
这是我从View指向的另一个模型
课程大师模型
public function attributeLabels()
{
return [
'id' => Yii::t('course', 'ID'),
'course_code' => Yii::t('course', 'Course Code'),
'course_type' => Yii::t('course', 'Course Type'),
'course_title' => Yii::t('course', 'Course Title'),
'course_unit' => Yii::t('course', 'Course Unit'),
];
}
function getCourseCodes()
{
return ($this->course_code);
}
function getCourseTitles()
{
return ($this->course_title);
}
function getCourseTypes()
{
return ($this->course_type);
}
function getCourseUnits()
{
return ($this->course_unit);
}
这是我要在视图中显示的主要模型
课程注册详细信息模型
public function attributeLabels()
{
return [
'id' => Yii::t('course', 'ID'),
'course_registration_id' => Yii::t('course', 'Course Registration'),
'course_id' => Yii::t('course', 'Course Title'),
'student_id' => Yii::t('course', 'Student'),
'remark' => Yii::t('course', 'Remark'),
];
}
public function getCourseMaster()
{
return $this->hasOne(CourseMaster::className(), ['id' => 'course_id']);
}
我希望能够将数组索引用作$ key
查看
<tr>
<td colspan=3 class="padding-left padding-right">
<?php $totalUnit = 0;
$courseRegistrationDetails = \app\modules\course\models\CourseRegistrationDetail::find()->where(['course_registration_id' => $model->id])->asArray()->all(); ?>
<table border="1" class="table table-border" style="width:100%;">
<tr class="header">
<th><?php echo Yii::t('report', 'SI.No'); ?></th>
<th><?php echo Yii::t('report', 'Course Code'); ?></th>
<th><?php echo Yii::t('report', 'Course Title'); ?></th>
<th><?php echo Yii::t('report', 'Course Type'); ?></th>
<th><?php echo Yii::t('report', 'Unit'); ?></th>
<th><?php echo Yii::t('report', 'Remark'); ?></th>
</tr>
<?php
foreach($courseRegistrationDetails as $key=>$value) {
echo '<tr>';
echo '<td class="text-center">'.($key+1).'</td>';
echo '<td class="text-center">'.$value->courseMasters->courseCodes.'</td>';
echo '<td class="text-center">'.$value->courseMasters->courseTitles.'</td>';
echo '<td class="text-center">'.$value->courseMasters->courseTypes.'</td>';
echo '<td class="text-center">'.$value->courseMasters->courseUnits.'</td>';
echo '<td class="text-center">'.$value['remark'].'</td>';
echo '</tr>';
$totalUnit+=$value['course_unit'];
}
?>
<tr>
<th class="text-right border-hide padding-right" colspan=4><?php echo Yii::t('report', 'Total Unit'); ?></th>
<th><?php echo $totalUnit; ?></th>
</tr>
</table>
</td>
</tr>
如何解决该错误并将数组索引用作$ key
答案 0 :(得分:0)
查询
$courseRegistrationDetails = \app\modules\course\models\CourseRegistrationDetail::find()
->with('courseMaster')
->where(['course_registration_id' => $model->id])
->all();
查看
<?php foreach ($courseRegistrationDetails as $key => $value) : ?>
<tr>
<td class="text-center"> <?= ($key + 1) ?> </td>
<td class="text-center"> <?= $value->courseMaster->courseCodes ?> </td>
<td class="text-center"> <?= $value->courseMaster->courseTitles ?> </td>
<td class="text-center"> <?= $value->courseMaster->courseTypes ?> </td>
<td class="text-center"> <?= $value->courseMaster->courseUnits ?> </td>
<td class="text-center"> <?= $value->remark ?> </td>
</tr>
<?php $totalUnit += $value->course_unit; ?>
<?php endforeach; ?>
使用asArray()
查询
$courseRegistrationDetails = \app\modules\course\models\CourseRegistrationDetail::find()
->with('courseMaster')
->where(['course_registration_id' => $model->id])
->asArray()
->all();
查看
<?php foreach ($courseRegistrationDetails as $key => $value) : ?>
<tr>
<td class="text-center"> <?= ($key + 1) ?> </td>
<td class="text-center"> <?= $value['courseMaster']['course_code'] ?> </td>
<td class="text-center"> <?= $value['courseMaster']['course_title'] ?> </td>
<td class="text-center"> <?= $value['courseMaster']['course_type'] ?> </td>
<td class="text-center"> <?= $value['courseMaster']['course_unit'] ?> </td>
<td class="text-center"> <?= $value['remark'] ?> </td>
</tr>
<?php $totalUnit += $value['course_unit']; ?>
<?php endforeach; ?>