$model = ForumSection::find()->with([
'topics' => function($query) {
$query->addSelect('*, COUNT(message_id) AS cnt_messages')->leftJoin('forumTopicMessage', 'topic=topic_id')->groupBy('topic_id');
}
])->all();
<ul>
<? foreach($model as $s) : ?>
<li><?= $s->section_title ?></li>
<ul>
<? foreach($s->topics as $t) : ?>
<li><?= $t->topic_title ?> | <?= $t->getMessagesCount() ?></li>
<? endforeach; ?>
</ul>
<? endforeach; ?>
</ul>