Yii2 - 关键值不起作用
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Yii2 - 关键值不起作用相关的知识,希望对你有一定的参考价值。
我想与另一个模型的模型相关,但是我收到了这个错误:
这是我从View中指出的另一个模型
CourseMaster模型
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);
}
这是我想在视图中显示的主模型
CourseRegistrationDetail模型
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 = appmodulescoursemodelsCourseRegistrationDetail::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
答案
询问
$courseRegistrationDetails = appmodulescoursemodelsCourseRegistrationDetail::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 = appmodulescoursemodelsCourseRegistrationDetail::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; ?>
以上是关于Yii2 - 关键值不起作用的主要内容,如果未能解决你的问题,请参考以下文章