Answer the question
In order to leave comments, you need to log in
foreach Yii2 -> Getting unknown property - eager loading?
Hello!
I implement eager loading from the DB
Controller:
$query = Product::find()->where(['category_id' => $id])->with('size');
$pages = new Pagination(['totalCount' => $query->count(), 'pageSize' => 3, 'forcePageParam' => false, 'pageSizeParam' => false]);
$products = $query->offset($pages->offset)->limit($pages->limit)->all();
return $this->render('view', compact('products', 'pages', 'category'));
public function getSize(){
return $this->hasMany(Size::className(), ['product_id' => 'id']);
}
public function getProducts(){
return $this->hasMany(Product::className(), ['id' => 'product_id']);
}
<?php
foreach($products as $product) {
echo "<h2>Название : " . $product->name . "</h2>";
echo "<ul>";
foreach($product->size as $sizes) { // no query executed here
echo "<li>" . $sizes->$size_p . "</li>";
}
echo "</ul>";
}
?>
Getting unknown property: app\models\Size::
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question