S
S
sdgs4s4 .2017-10-22 15:56:46
Yii
sdgs4s4 ., 2017-10-22 15:56:46

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

Product Model
public function getSize(){
        return $this->hasMany(Size::className(), ['product_id' => 'id']);
    }

Model Size
public function getProducts(){
        return $this->hasMany(Product::className(), ['id' => 'product_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>";
}
?>

Gives an error messageGetting unknown property: app\models\Size::
DB in pictures
59ec9540ebb0e845543350.png59ec9546dbc89509541090.png

And if necessary - a picture of the error
Zhmyak
59ec957d9c1b6811921809.png

The second day I dance with a tambourine, I still can’t understand where the mistake is, help me out

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artem, 2017-10-22
@proudmore

var_dump $product to studio. If everything is correct, then you will have your access model by the size key directly in the object.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question