Retrieve data from multiple tables in a list

Hello folks,

For a while I’ve been thinking how to do this with Yii the best way, I know how to do it in traditional PHP but I’m not sure if it’s efficient enough and I also thought that Yii had an easier way of doing this. This is what I’m trying to do:

I have two tables in my database: Groups and Items, they are both relational and both have relations configured in their models. Items is an entity of the table Groups.

I want to display all of the groups from the database with a list of all the items that are in it on one single page. So basically the multiple lists next to each other like:

GroupA GroupB GroupC


Item1 ItemA ItemI

Item2 ItemB ItemII

Item3 ItemC ItemIII

Item4 ItemD ItemIV

If anyone has done this or has an idea of how to script this the best way, I’d appreciate if you could leave a reply here with your logic. Thanks in advance :)

If U understood right what U are trying to do adn if U have relations set up in model class:

$groups = Groups::model()->findAll(); // to get all groups

foreach ($groups as $group)

{

echo $group->name;

echo ‘<hr/>’;

foreach ($gruop->relationToItems as $gruopItem) // go trough all group items

{

   echo &#036;gruopItem-&gt;name;

}

}

U will probably want to change html, but I think that this will show U what is idea.

1 Like

Thanks mate, I haven’t thought of that, can’t be more simple than that :)