Open to suggestions, this is just how I did it. So Lets say that I have two tables, Person and a FavoriteFood.
Create a method inside Person.php to access the FavoriteFood.php model.
Person.php
...
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'favorite_food' => array(self::BELONGS_TO,'FavoriteFood','favoriteFoodId'),
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'Id',
'firstName' => 'First Name',
'lastName' => 'Last Name',
'favoriteFoodId' => 'Favorite Food',
);
}
public function getFavoriteFoodList()
{
$food = new FavoriteFood;
$foodAll = $food->findAll(); //query database to return all records
return $listFoodAll = CHtml::listData($foodAll,'id','name'); // listData() returns an object for use with drop downs, $id = key, name = $value
}
...
PersonController.php
...
public function actionCreate()
{
$model=new Person;
$listFoodAll = Person::model()->getFavoriteFoodList();
if(isset($_POST['Person']))
{
$model->attributes=$_POST['Person'];
if($model->save())
$this->redirect(array('view','id'=>$model->id));
}
$this->render('create',array(
'model'=>$model,
'listFoodAll'=>$listFoodAll,
));
}
...
../views/person/create.php
<?php
$this->breadcrumbs=array(
'Person'=>array('index'),
'Create',
);
?>
<h1>Create Person</h1>
<ul class="actions">
<li><?php echo CHtml::link('List Person',array('index')); ?></li>
<li><?php echo CHtml::link('Manage Person',array('admin')); ?></li>
</ul><!-- actions -->
<?php echo $this->renderPartial('_form', array('model'=>$model,'listFoodAll'=>$listFoodAll)); ?>
../views/person/_form.php
<div class="form">
<?php echo CHtml::beginForm(); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo CHtml::errorSummary($model); ?>
<div class="row">
<?php echo CHtml::activeLabelEx($model,'firstName'); ?>
<?php echo CHtml::activeTextField($model,'firstName',array('size'=>60,'maxlength'=>100)); ?>
<?php echo CHtml::error($model,'firstName'); ?>
</div>
<div class="row">
<?php echo CHtml::activeLabelEx($model,'lastName'); ?>
<?php echo CHtml::activeTextField($model,'lastName',array('size'=>60,'maxlength'=>100)); ?>
<?php echo CHtml::error($model,'lastName'); ?>
</div>
<div class="row">
<?php echo CHtml::activeLabelEx($model,'favoriteFoodId'); ?>
<?php echo CHtml::activeDropDownList($model,'favoriteFoodId',$listFoodAll); ?>
<?php echo CHtml::error($model,'favoriteFoodId'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php echo CHtml::endForm(); ?>
</div><!-- form -->