$model = Images::find() ->where(['product_id' => $id]) ->joinWith(['product' => function ($q) { $q->from('product'); }]) ->all(); if (Yii::$app->user->can('updateFabricatorImages', ['brand_id' => $model->product->brand_id])) { code } Trying to get property 'product' of non-object 

What is the reason?

var_dump ($ model) ;:

 array(1) { [0]=> object(frontend\models\Images)#128 (11) { ["image"]=> NULL ["_attributes":"yii\db\BaseActiveRecord":private]=> array(5) { ["id"]=> int(5) ["url"]=> string(9) "115_0.jpg" ["product_id"]=> int(115) ["deleted"]=> NULL ["main"]=> NULL } ["_oldAttributes":"yii\db\BaseActiveRecord":private]=> array(5) { ["id"]=> int(5) ["url"]=> string(9) "115_0.jpg" ["product_id"]=> int(115) ["deleted"]=> NULL ["main"]=> NULL } ["_related":"yii\db\BaseActiveRecord":private]=> array(1) { ["product"]=> object(frontend\models\Products)#137 (10) { ["_attributes":"yii\db\BaseActiveRecord":private]=> array(5) { ["id"]=> int(115) ["name"]=> string(2) "wr" ["created_at"]=> int(1537185812) ["updated_at"]=> int(1537431514) ["brand_id"]=> int(4) } ["_oldAttributes":"yii\db\BaseActiveRecord":private]=> array(5) { ["id"]=> int(115) ["name"]=> string(2) "wr" ["created_at"]=> int(1537185812) ["updated_at"]=> int(1537431514) ["brand_id"]=> int(4) } ["_related":"yii\db\BaseActiveRecord":private]=> array(0) { } ["_relationsDependencies":"yii\db\BaseActiveRecord":private]=> array(0) { } ["_errors":"yii\base\Model":private]=> NULL ["_validators":"yii\base\Model":private]=> NULL ["_scenario":"yii\base\Model":private]=> string(7) "default" ["_events":"yii\base\Component":private]=> array(2) { ["beforeInsert"]=> array(1) { [0]=> array(2) { [0]=> array(2) { [0]=> object(yii\behaviors\TimestampBehavior)#138 (7) { ["createdAtAttribute"]=> string(10) "created_at" ["updatedAtAttribute"]=> string(10) "updated_at" ["value"]=> NULL ["attributes"]=> array(2) { ["beforeInsert"]=> array(2) { [0]=> string(10) "created_at" [1]=> string(10) "updated_at" } ["beforeUpdate"]=> string(10) "updated_at" } ["skipUpdateOnClean"]=> bool(true) ["preserveNonEmptyValues"]=> bool(false) ["owner"]=> *RECURSION* } [1]=> string(18) "evaluateAttributes" } [1]=> NULL } } ["beforeUpdate"]=> array(1) { [0]=> array(2) { [0]=> array(2) { [0]=> object(yii\behaviors\TimestampBehavior)#138 (7) { ["createdAtAttribute"]=> string(10) "created_at" ["updatedAtAttribute"]=> string(10) "updated_at" ["value"]=> NULL ["attributes"]=> array(2) { ["beforeInsert"]=> array(2) { [0]=> string(10) "created_at" [1]=> string(10) "updated_at" } ["beforeUpdate"]=> string(10) "updated_at" } ["skipUpdateOnClean"]=> bool(true) ["preserveNonEmptyValues"]=> bool(false) ["owner"]=> 

    2 answers 2

    You do ->all() that returns an array of objects. And you are trying to get an object property from the array: $model->product->brand_id . Even var_dump prints an array to you.

    • What do I need to do to get the desired property from a linked table? - dasauser
    • Hmm .. ->one() - robertobadjio
    • Join does not work with one - dasauser
    • To your question "What is the reason?" I answered, then see for yourself - robertobadjio
    • Do you need a join there if you need one? The meaning of join is to blind one table of the two. If one is needed, then simply by the relation we get the necessary data. There is essentially a join from the box. - fedornabilkin
      $model = Products::find() ->joinWith('images') ->asArray() ->all();