A
A
AlexSer2018-08-13 09:19:09
Yii
AlexSer, 2018-08-13 09:19:09

How to count the number of records in related tables, via ActiveRecord?

I have three tables.
table1->hasMany(table2)->hasMany(table3);
How it is possible to receive an amount of records in the third table if to make in the first selection under conditions.?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Timofeev, 2018-08-13
@AlexSer

class User ....
    public function getNapravlenie(){
        return $this->hasMany(Napravlenie::className(),['historyID'=>'id']);
    }

    public  function  getAnaliz(){
        return $this->hasMany(CheckAnaliz::className(), ['napravlenieID' => 'id'])->via('napravlenie');
    }

   public  function  getAnalizCount(){
        return count($this->analiz);
    }
....

somewhere
$users = Users::find()->with(['analiz','napravlenie'])->all();
foreach($users as $one){
    echo $one->id . " = " . $one->analizCount;
}

E
Evgeny Bukharev, 2018-08-13
@evgenybuckharev

joinWith() && count()
Task context and code examples are needed, it's hard to tell anything without them

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question