A
A
Alexander Stepanov2018-02-23 20:20:53
Yii
Alexander Stepanov, 2018-02-23 20:20:53

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}",
]); ?>

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

In controller:
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
        ]);
    }

In the category model:
public function getProducts()
    {
        return $this->hasMany(Product::className(), ['category_id' => 'id']);
    }

Relationship with the table of designers in the table of products
public function getDesigner()
    {
        return $this->hasOne(Designer::className(), ['id' => 'designer_id']);
    }

In other places, the designer is pulling up, but not in the category.
Category search model has with('products') Product search model has with(['category', 'designer'])
How to make a designer an object everywhere?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Stepanov, 2018-02-24
@Exebeche

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 question

Ask a Question

731 491 924 answers to any question