I had similar problem when I started with yii2
from A you have to have relation to B, from B to C and from C to D
connection , like this on all models. So the function will look like this in A it will look like this( I added in A model main ID wich is id_A):
public function B
{
return $this->hasMany(B::className(), ['a_id' => 'id_A'])
}
The same will be for B to C, in model B you have:
public function C
{
return $this->hasMany(C::className(), ['id_C' => 'c_id'])
}
Remember this connection C::className(), ['id_C' => 'c_id']
is from column from next model you need to this model where you are, data have to be related.
After this you need to set app the search model for the main model( A ). The search have to look like this. First you add the in A search model:
public $id_C
Public $c_id ...
all variables which you will add from other models. And after this add them in firs row in
public function rules()
{
return [
[[......, 'id_C','c_id'...(all var you need) ]]
.....] }
After you add all of them in first row(it will make them safe) go to
public function search($params)
{ $query = A::find()
->select(...)
->where (...)
->joinWith(B)->joinWith(A.B)->joinWith(A.B.C).... how many you need.
;