Yehia Awad Yehia Awad - 4 months ago 51
MySQL Question

Yii2 activerecord PHP get multiple join from 3 different Tables

I have 3 Models

class ModelA{
public function getBs{
return $this->hasMany(ModelB::className(), ["AID"=>"BID"]);
}
}

class ModelB{
public function getCs{
return $this->hasMany(ModelC::className(), ["BID"=>"CID"]);
}
}

class ModelC{

}


using yii2 activerecord I'd like to retrieve the relations of
ModelB, ModelC
tables joined with
ModelA


I tried the following:

$a = ModelA::find()
->joinwith("bs")
->joinwith("as")
->where(["id"=>1])


I could get from this
$a->bs
but not
'$a->bs->cs'
,

I could have use some other way that would lazy load the data from db but I am looking for EagerLoading data from db

what am I doing wrong here ? Thanks for your help in advance

Answer

It's explained in the guide: Lazy Loading and Eager Loading

$a = ModelA::find()->with('bs.cs')->all();
Comments