Yehia Awad Yehia Awad - 1 year ago 167
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

I tried the following:

$a = ModelA::find()

I could get from this
but not

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 Source

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

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