Answer the question
In order to leave comments, you need to log in
Why is the category page not displaying related content?
I'm trying to display products on the category page, but I get an error Trying to get property of non-object
swears at the line <?= $model->designer->brand_name ?>
I display through the listView
<?= ListView::widget([
'dataProvider' => $dataProvider,
'itemView' => '_view',
'itemOptions' => ['class' => 'item'],
'viewParams' => ['testParam' => 'designer.brand_name'],
'layout' => "{items}\n{pager}",
]); ?>
<div class="col-md-4 col-sm-6 col-xs-12 productItem">
<a href="<?= Url::toRoute(['shop/view', 'id'=>$model->id]); ?>">
<div class="productItem_image-container" style="background-image: url('<?= $imgs[0]->getUrl('300x400'); ?>')">
<?php if (isset($imgs[1])): ?>
<img class="productItem-image" src="<?= $imgs[1]->getUrl('300x400'); ?>" alt="">
<?php else: ?>
<img class="productItem-image" src="<?= $imgs[0]->getUrl('300x400'); ?>" alt="">
<?php endif; ?>
<?= $model->new ? '<p class="newLabel">new</p>' : '' ?>
<?= $model->sale ? '<p class="saleLabel">sale</p>' : '' ?>
</div>
<span class="productItem_text">
<p class="productItem_text-title">
<?= Html::encode($model->title) ?>
</p>
<p class="productItem_text-brand">
<?= $model->designer->brand_name ?>
</p>
<p class="productItem_text-price">
$ <?= Html::encode($model->price) ?>
</p>
</span>
</a>
<?= WishlistButton::widget([
'model' => $model, // модель для добавления
'anchorActive' => '<i class="fas fa-heart"></i>', // свой текст активной кнопки
'anchorUnactive' => '<i class="far fa-heart"></i>', // свой текст неактивной кнопки
'htmlTag' => 'a', // тэг
'cssClass' => 'wish', // свой класс
'cssClassInList' => 'active' // свой класс для добавленного объекта
]) ?>
</div>
public function actionCategory($id)
{
$searchModel = new ProductSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$categories = Category::find()->asArray()->indexBy('id')->all();
return $this->render('category', [
'dataProvider' => $dataProvider,
'searchModel' => $searchModel,
'categories' => $categories
]);
}
public function getProducts()
{
return $this->hasMany(Product::className(), ['category_id' => 'id']);
}
public function getDesigner()
{
return $this->hasOne(Designer::className(), ['id' => 'designer_id']);
}
Answer the question
In order to leave comments, you need to log in
Everything was resolved by replacing the code with <?= $model->designer['brand_name'] ?>
The most interesting thing is that the output of this line in this project changed from <?= $model->designer['brand_name'] ?>
to <?= $model->designer->brand_name ?>
and back several times ...
I would also like to understand why?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question