Thanks for the example Mohsin. I ended up doing something very similar but… but it does seem like there should be a better way.
This is what I did:
public function actionIndex()
{
$criteria=new CDbCriteria;
$criteria->select='DISTINCT t.title';
$criteria->condition='t.active=1';
$criteria->order='menu_position ASC';
$criteria->join='INNER JOIN menu ON menu.menu_category_id=t.id';
$category=new CActiveDataProvider('MenuCategory', array(
'criteria'=>$criteria,
));
$criteria=new CDbCriteria;
$criteria->condition='t.active=1';
$criteria->order='category.menu_position ASC, category_position ASC';
$criteria->with=array('category');
$menu=new CActiveDataProvider('Menu', array(
'criteria'=>$criteria,
));
$this->render('index',array(
'dataProvider'=>$menu,
'categories'=>$category,
));
}
First I made a data provider with unique categories that have also been used in the menu table. Then made a data provider with my menu and their respective category.
Nothing wrong with this solution. Only thing I’d add is a with(), and maybe a together(), in with the find. That way it only performs one SQL query rather than one per category. As this can avoid some query overhead and be quicker, more noticeable if you connect over TCP though.
I am doing something similar to this problem. I have a table Comic in which I stores: ComicID (PK) and Comic_Title. And I want to display all available comics grouped by the first letter of comic’s title like this:
A.
Air Craft [specific space] Alibaba [specific space] Amma
B.
Boys [specific space] Boys and Girls [specific space] BBB
Can anyone suggest me a way to display comics like that?