Say your model name is "User", and it has properties id, name and phone. I do this sort of thing often. This is untested code, but you get the idea:
[php]
// ----------------------------------------------
// User model. Write a function to return the dropdown list.
// ----------------------------------------------
public static function getOptions(l) {
$criteria=new CDbCriteria;
$criteria->select = ‘id, name’; // ONLY select columns needed!
$criteria->order = ‘name’;
$models = self::model()->findAll($criteria);
return CHtml::listData($models, ‘id’, ‘name’);
}
// ----------------------------------------------
// View. This code assumes that your $form is representing
// the selected user with a field called ‘user_id’
// ----------------------------------------------
echo CHtml::activeDropDownList($form, ‘user_id’, User::getOptions(),
array(
'style' => 'width:280px;',
'empty' => 'Choose user >',
'ajax' => array(
'type' => 'POST',
'url' => $this->createUrl('selectUser'),
'dataType' => 'json',
'success' => 'function(data) { selectUser(data); }',
'error' => 'function(data) { alert("error!"); }',
)));
// ----------------------------------------------
// Controller
// ----------------------------------------------
/**
* Ajax function, called by _form.php view.
-
When a user has been selected, populate the phone number div.
*/
public function actionClone() {
$userId = $_POST['UserForm']['user_id'];
// Grab User instance
// userId will be null if no user was selected
if (empty($userId)) {
$event = null;