Yii2 - Key Value Not Working

Yii2 - Key Value Not Working



I want to relate to a model from another model, but I got this error:



Composite key



This is the other model I am pointing to from the View



CourseMaster Model


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



This is the Main Model that I want to display in the view



CourseRegistrationDetail Model


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']);



I want to be able to use array index as $key



View




<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>



How do I resolve the error and use array index as $key





It's hard to say what the problem is exactly but it looks like $value->courseMasters might be returning null? $courseRegistrationDetails is an array and $key and $value look like they should be correctly populated.
– tschumann
Aug 24 at 1:58





Your relation name is courseMaster not courseMasters. Secondary you have used asArray() in query, so cannot access object or relation that way.
– Insane Skull
Aug 24 at 4:26



courseMaster


courseMasters


asArray()





if you are using asArray()in query try $value['courseMasters']['courseCodes']
– Gru
Aug 24 at 4:59


asArray()


$value['courseMasters']['courseCodes']




1 Answer
1



Query


$courseRegistrationDetails = appmodulescoursemodelsCourseRegistrationDetail::find()
->with('courseMaster')
->where(['course_registration_id' => $model->id])
->all();



View


<?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; ?>



Using asArray()



Query


$courseRegistrationDetails = appmodulescoursemodelsCourseRegistrationDetail::find()
->with('courseMaster')
->where(['course_registration_id' => $model->id])
->asArray()
->all();



View


<?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; ?>






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)