Answer the question
In order to leave comments, you need to log in
Yii2 eager loading error - "Invalid argument" - help me out!?
Hello!
Controller:
$query = Product::find()->where(['category_id' => $id])->with('size');
$products = $query->offset($pages->offset)->limit($pages->limit)->all();
return $this->render('view', compact('products', 'pages', 'category'));
public function getSize(){
return $this->hasOne(Sizes::className(), ['id' => 'size_id']);
}
public function getProducts(){
return $this->hasMany(Product::className(), ['size_id' => '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>";
}
?>
Answer the question
In order to leave comments, you need to log in
Well, here it is written HASONE
public function getSize(){
return $this->hasOne(Sizes::className(), ['id' => 'size_id']);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question