How do I resolve trying to get property of non-object in relations. as shown below
7189
The problem is trying to use
<?php echo $cvalue->scoredItemCategory->scored_item_category_name ?>
instead of
<?php echo $cvalue['scored_item_category_name'] ?>
The relations are in the model
Model
public function getScoredItemSubCategory()
{
return $this->hasOne(ScoredItemSubCategory::className(), ['scored_item_sub_category_id' => 'scored_item_sub_category_id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getScoredItemCategory()
{
return $this->hasOne(ScoredItemCategory::className(), ['scored_item_category_id' => 'scored_item_category_id']);
}
public function getTariff()
{
return $this->hasOne(BuildingTariff::className(), ['tariff_id' => 'tariff_id']);
}
public function getBuildingTariff()
{
return $this->hasOne(BuildingTariff::className(), ['tariff_id' => 'tariff_id']);
}
Controller
$rating = new InspectionRatingScale();
$scoreditem = new Scoreditem();
$scoreditems=$scoreditem->getBuildingScoreditem($building_id);
$scoreditemscore = new ScoredItemScore();
$scoreditemscore1=$scoreditemscore->getScore($building_id,$bip_id);
if(isset($_POST['InspectionRatingScale']))
{
foreach ($_POST['InspectionRatingScale']['scoreditem'] as $key=>$value)
{
$scoreditemscore = ScoredItemScore::find()->where(['building_id' => $building_id, 'bookinspection_id' => $bip_id, 'item_id' => $key])->one();
if(!isset($scoreditemscore))
$scoreditemscore = new ScoredItemScore();
$scoreditemscore->item_id=$key;
$scoreditemscore->rating=$value['rating_text'];
$scoreditemscore->date_inspected=Date("Y/m/d");
$scoreditemscore->date_updated= Date("Y/m/d");
$scoreditemscore->scored_comment= $key['scored_comment'];//$_POST['ScoredItemScore']['[$key]scored_comment']; $_POST['ScoredItemScore']['scored_comment'][$key];
$scoreditemscore->building_id=$building_id;
$scoreditemscore->bookinspection_id=$bip_id;
$scoreditemscore->inspector_id=$inspector_id;
$scoreditemscore->save();
}
VIEW
$i = 1;
$key = 0;
if(count($scoreditemscore)>=1){
foreach ($scoreditemscore as $cvalue)
{
?>
<tr>
<td><?php echo $i++;?></td>
<td width="300"><?php echo $cvalue->scoredItemCategory->scored_item_category_name ?></td>
<td width="300"><?php echo $cvalue->scoredItemSubCategory->scored_item_sub_category_name ?></td>
<td><?php echo $cvalue['scored_item_activity'] ?></td>
<td width="300">
<?php echo $form->field($scoreditemscore, '[$cvalue]scored_comment')->textInput()->label(false); ?>
</td>
<td style="display:none;">
<?php
$name="InspectionRatingScale[scoreditem][$cvalue[item_id]][rating_text]";
$yn=array("$cvalue[rating]"=>array('selected'=>true));
//echo $form->dropDownList($rating, 'rating_text', ArrayHelper::map($rating->findAll(),'rating_value','rating_text'),array('class'=>'form-control','name'=>$name,'options'=>$yn))->label(false);
echo $form->field($rating, 'rating_text')->dropDownList(ArrayHelper::map(\app\modules\inspection\models\InspectionRatingScale::find()->all(),'rating_value','rating_text'), ['name'=>$name,'options'=>$yn])->label(false);
?>
</td>
</tr>
Thanks