S
S
sdgs4s4 .2017-10-21 21:26:55
Yii
sdgs4s4 ., 2017-10-21 21:26:55

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'));

Product Model:
public function getSize(){
        return $this->hasOne(Sizes::className(), ['id' => 'size_id']);
    }

Model Sizes
public function getProducts(){
        return $this->hasMany(Product::className(), ['size_id' => 'id']);
    }

View:
<?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>";
}
?>

I'm trying to display a dimensional grid by product id
DB pictures:
Zhmyak
59eb90b47cf07020767279.png59eb90bc37b96999253193.png
Error (Zhmyak)
59eb90e772e2d412920058.png

I don’t understand why foreach doesn’t work, help me out, boiled up!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Pavel Gogolinsky, 2017-10-21
@gogolinsky

Well, here it is written HASONE

public function getSize(){
        return $this->hasOne(Sizes::className(), ['id' => 'size_id']);
    }

Means foreach here it is impossible to pass. $product->size is not an array, but an object

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question