Newbie stuck with using multiple tables

I really like Yii and want to rewrite everything I’ve done in the past… but I’m having a mental block doing it. I’ll layout the tables I have and the way I did it previous to Yii and the way I’m attempting to do it. I’m aware that I’m not using any relations, which might be the root of all my problems.

First, 3 table structures:


CREATE TABLE `companyinfo` (

  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,

  `userID` int(11) unsigned NOT NULL DEFAULT '0',

  `companyName` varchar(100) NOT NULL DEFAULT '',

  PRIMARY KEY (`id`)

) ENGINE=InnoDB ;


CREATE TABLE `categories` (

  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,

  `name` varchar(255) NOT NULL DEFAULT '',

  PRIMARY KEY (`id`)

) ENGINE=InnoDB ;


CREATE TABLE `categories_relation` (

  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,

  `userID` int(11) unsigned NOT NULL DEFAULT '0',

  `selCategoryID` int(11) unsigned NOT NULL DEFAULT '0',

  PRIMARY KEY (`id`),

) ENGINE=InnoDB ;



On the page, I display a drop down list with the contents of categories (id, name), the visitor selects a category (name), then I look in categories_relation for the userID’s that match and then pull up the companyinfo.userID that matches the categories they’ve selected.

Now in Yii, I created models for each table. I’d like to achieve the same thing I’ve done before, but in Yii. So far, I have this:

In CompanyinfoController.php:


	/**

	 * Lists all models.

	 */

	public function actionIndex()

	{

		$dataProvider=new CActiveDataProvider('CompanyInfo', array(

			'criteria'=>array(

			),

			'sort'=>array(

				'defaultOrder'=>'companyName',

			),

			'pagination'=>array(

				'pageSize'=>10,

			),

		));

		

		$this->render('index',array(

			'dataProvider'=>$dataProvider,

		));

	}



What I want to happen in the view is to show the drop down list with the categories listed from the table categories and the companies below that, by default all companies are shown until they select a category. I just need a frame in Yii to do this, I can’t seem to wrap my head around it.

Should I do something with relations() in the CompanyInfo.php model and if so, what? Or can I just extend the model to include categories?

Thanks for any and all help.

You should definitely define and use relations. Take a look at http://www.yiiframework.com/doc/guide/1.1/en/database.arr

Also, for the dropdown, you can use dependent dropdowns like this wiki points out: http://www.yiiframework.com/wiki/24/