A better way to sort related data

I’ve only been working with Yii for about 2 weeks, so I’m hoping that some of you can help me with this problem.

I have 3 models: item, itemCompany, and company. itemCompany has 2 FKs that tie in item and company (named itemId and companyId respectively). What I’m trying to do right now is upon accessing company, I want to retrieve all the items associated with said company and sort the names of the items (item.name) in alphabetical order.

What I’m doing is in the Company controller, I’m retrieving the itemCompany association and passing it along to the view and having the view retrieve the associated item.name by running the getItemText() method in ItemCompany when it needs something to output.

I’m trying to find the best way to do this (best practices, specifically) and I’m thinking that I can just grab all the associated itemIds from the $itemCompanyDataProvider then call another CActiveDataProvider that calls an instance of Item and get all the itemIds and names and sort it. If at all possible I’m trying to use the built in Yii functions and methods.

Currently, I have this going on in each of the files:


public function actionView($id)


		$model = $this->loadModel($id);

		$itemCompanyDataProvider=new CActiveDataProvider('ItemCompany',array('criteria'=>array('condition'=>'companyId=:companyId','params'=>array(':companyId'=>$model->companyId)),'pagination'=>array('pageSize'=>25)));







public function getItemOptions()


		$itemModel = Company::model()->findAll(array('order'=>'name'));

		$itemArray = CHtml::listData($itemModel, 'itemId', 'name');

		return $itemArray;



	public function getItemText()


		$itemOptions = $this->itemOptions;

		return isset($itemOptions[$this->itemId]) ? $itemOptions[$this->itemId] : "Unknown item ({$this->itemId})";



<?php $this->widget('zii.widgets.CListView', array('dataProvider'=>$itemCompanyDataProvider,'itemView'=>'/itemCompany/_view1',)); ?>


<div class="view">

	<?php echo CHtml::link(CHtml::encode($data->getItemText()), array('item/view', 'id'=>$data->itemId)); ?>

	<br />


Thanks in advance for the help!