问题描述
我正在玩一个小应用程序以学习使用 Yii.
我创建了一个带有 2 个模型/表的小型 web 应用程序:项目和任务.(一对多关系,在模型类中正确配置).
我现在正在尝试自定义任务/创建"视图,将文本输入字段替换为一个提供可用项目列表的选择框.
我打开了表单视图并尝试了这个:
<?php echo $form->labelEx($model,'project_id');?><?php echo $form->textField($model,'project_id');?><?php//我的黑客从这里开始$projects = Project::model()->findAll();$list = CHtml::listData($projects, 'id', 'name');echo $form->listBox($model,'project_id','', $list);?>//我的 hack 到此结束<?php echo $form->error($model,'project_id');?>
但它不断抛出警告或错误(例如为foreach()提供的无效参数
,并且绝对不起作用.我不明白我做错了什么.你能帮忙吗??
你的论点不合理(应该是):
$frameworks = Framework::model()->findAll();$list = CHtml::listData($frameworks, 'id', 'name');echo $form->listBox($model,'framework_id', $list,array());
查看文档
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 ?
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
这篇关于Yii:如何使用另一个模型数据填充 Select 输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!