Answer the question
In order to leave comments, you need to log in
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'));
}
echo LinkPager::widget([
'pagination' => $pages,
]);
$image = $productss->getImage();
echo $image->getUrl();
Answer the question
In order to leave comments, you need to log in
Don't reinvent the wheel, half of your code is not needed if you use a ready-made ActiveDataProvider
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 ->asArray
or some ArrayHelper::map
in afterfind, for example, or somewhere else.
And further:
$image = $productss->getImage();
echo $image->getUrl();
foreach($productss as $one){
echo $one->getImage()->getUrl();
}
foreach($productss as $one){
echo $one->image->getUrl();
}
foreach($productss as $one){
echo $one->getImage()->one()->getUrl();
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question