class AuthorController extends Controller {
public function actionIndex() {
$authors = Author::find()->select(['author.id', 'author.name', 'books' => 'count(*)'])
->leftJoin('book', 'author.id = book.author_id')
->groupBy('author.id')->asArray()->all();
return $this->render('index', ['authors' => $authors]);
}
}
foreach($authors as $author){
echo '<h2>'.$author['id'].') '.$author['name'].'</h2>'; // 1) Иван И.И.
echo '<p>Книг: '.$author['books'].'</p>'; // Книг: 17
}