R
R
Ruslan2017-05-13 19:35:11
Yii
Ruslan, 2017-05-13 19:35:11

Problems with displaying images when using pagination?

Hello dear experts. I have the following problem:
I do pagination to display products on the page in the following way in the controller:

public function actionView_category_products()
    {
        $id=Yii::$app->request->get('id_category');
        $category=Category::findOne($id);
        if(empty($category)){
            throw new HttpException(404,'Selection of the category does not exist!');
        }


        $request=Products::find()->where(['category_id'=>$id]);
        $pages=new Pagination([
                            'totalCount' => $request->count(),
                            'pageSize'=>9,
                            'forcePageParam'=>false,
                            'pageSizeParam'=>false,
        ]);
        $productss=$request->offset($pages->offset)->limit($pages->limit)->all();

        //mas_print($productss);
        $count_record=$request->count();//передача кількості знайдених товарів
        $this->setMeta('E-Shoper|Category: '.$category->name,$category->keywords,$category->description);
        return $this->render('view_category_products',compact('productss','pages','count_record', 'category'));
    }

in its own form like this:
echo LinkPager::widget([
                                'pagination' => $pages,
                            ]);

Working with pictures is implemented through yii2-images . Where to display the product image the following code is used:
$image = $productss->getImage();
 echo $image->getUrl();

But the code $request->offset($pages->offset)->limit($pages->limit)->all(); returns an array in $productss, consisting of arrays, not objects.
Please tell me how can I solve this problem. In particular, I'm interested in how to implement pagination so that it returns an array of objects. Or how not to load the server with unnecessary requests to pull out a picture. Thank you all in advance.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Fedorov, 2017-05-15
@qonand

Don't reinvent the wheel, half of your code is not needed if you use a ready-made ActiveDataProvider

M
Maxim Timofeev, 2017-05-15
@webinar

I support Maxim Fedorov 100% and add:
can not be. ->all()returns an array of objects. And if you really got an array without objects, then it was either lost somewhere ->asArrayor some ArrayHelper::mapin afterfind, for example, or somewhere else.
And further:

$image = $productss->getImage();
echo $image->getUrl();

should not work, since there is an array, not an object, which means either
or
foreach($productss as $one){
echo $one->getImage()->getUrl();
}

And if you also have an Image connection, then:
or
foreach($productss as $one){
echo $one->image->getUrl();
}

or
foreach($productss as $one){
echo $one->getImage()->one()->getUrl();
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question