I
I
Isherath2018-05-23 17:39:40
MySQL
Isherath, 2018-05-23 17:39:40

How to get the number of rows in a bunch of Yii2 tables?

I'm working on a project using the Yii2 framework.
Essence:
There are three tables in a DB. In ActiveRecord, they are linked sequentially: the Section table is linked to Topic (hasMany), the Topic table is linked to Message (also hasMany).
I need to get a list of all Sections, each of which will contain a list of Topics belonging to Sections + immediately the number of all Messages for each Topic.
It would seem that the task is not difficult, but for some reason I have been staring at the documentation for the second day and I can’t figure out how to implement this using Yii2 ActiveRecord.
I would be grateful if someone will respond and tell me what's what.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mysterion, 2018-05-23
@Mysterion

$model = Section::find()->joinWith(['topicRelationMethod','topicRelationMethod.messageRelationMethod'])->all();

replace topicRelationMethod and topicRelationMethod.messageRelationMethod with your own methods.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question