I am working with Yii framework 2.0 and have two database tables (A and B). It is a relational database 1:n. A has only one B but B has many of A. My database looks similar to this.
A table:
id = 1, name = yes
id = 2, name = no
id = 3, name = ok
id = 4, name = good
B table:
id = 1, a_id = 1
id = 2, a_id = 1
id = 3, a_id = 2
id = 4, a_id = 2
id = 5, a_id = 3
In my controller I use the following code to retrieve all data of A and B:
$bModel = B::find()->all();
In my view I use the following for-each to echo all the data.
foreach($bModel as $b) {
echo $b->a->name . ' - ' $b->id. '<br>;
}
The result is:
yes - 1
yes - 2
no - 3
no - 4
What I want is:
yes - 1
2
no - 3
4
I want to merge the a_id and show all the b's id of each merged a_id. I don't want to use the normal way by firstly get all a's id, use for-each to loop each a's id and then query b one by one by a's id. How can I approach the second result as I wanted with Yii framework 2.0?