Yii: How to populate a Select input with another model data?
relational-database, yii
Solution
Your arguments are not in order (it should be):
$frameworks = Framework::model()->findAll();
$list = CHtml::listData($frameworks, 'id', 'name');
echo $form->listBox($model,'framework_id', $list,array());
Check the documentation
Problem
I'm playing around with a small app in order to learn to use Yii. I've created a small webapp with 2 models / tables: Projects and tasks. (1-to-many relationship, properly configured in the model classes). I'm now trying to customize the Task/create view, replacing the text input field with a select box proposing the list of available projects. I opened the form view and tried this: ``` <div class="row"> <?php echo $form->labelEx($model,'project_id'); ?> <?php echo $form->textField($model,'project_id'); ?> <?php // my hack starts here $projects = Project::model()->findAll(); $list = CHtml::listData($projects, 'id', 'name'); echo $form->listBox($model,'project_id','', $list); ?> // my hack ends here <?php echo $form->error($model,'project_id'); ?> </div> ``` But it keeps throwing warnings or error (such as `Invalid argument supplied for foreach()`, and definitely does not work. I'm failing to understand what i'm doing wrong. Can you help ?