Answer the question
In order to leave comments, you need to log in
How to count the number of records using ActiveRecord in Yii2?
For example, there is a table Book and Author (Authors). A book, 1 and there can be several openings for 1 book.
Book
id| title|
Autor
id|book_id|name_auth
$count=Book::find()
->leftJoin('Author', '`author`.`book_id` = `book`.`id`')
->count();
Answer the question
In order to leave comments, you need to log in
A book, 1 and there can be several openings for 1 book.
$books = Book::find()
->with('author')
->all();
foreach($books as $book){
echo "количество авторов у книги " . $book->title . ": " . count($book->author);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question