there is such a request

$top_cats = ModArendaTree::find()->with('adscount')->where(['active' => 1])->andWhere(['parent_id' => 0])->orderBy(['popular' => SORT_DESC, 'sort' => SORT_DESC])->all(); 

determined the relationship in the model

 public function getAdscount(){ return $this->hasMany(CatsForAds2::className(),['ad_id'=>'id'])->count(); } 

however, it displays an error that there is no such connection.

 app\modules\Tree\models\ModArendaTree has no relation named "adscount". но есть в связи убираю "->count()" то все отлично работает... Почему так? 

    1 answer 1

    When building a query, the method ->with('adscount') searches ModArendaTree for a getAdscount() method that returns an instance of the ActiveQuery class.

    When you add ->count() , the method will try to return a number and not an instance of the class, so the builder simply does not find it.

    • and what should I do if I don’t need the whole table but only the number of records found? - Anatoly
    • A link is not the result of a query, it is roughly a description of join (although in this case there will be two queries). So use ->count() not in connection but in the results. - Bookin