English 中文(简体)
Yii: 如何用另一个模型数据填充“ 选择” 输入?
原标题:Yii: How to populate a Select input with another model data?

我用一个小的应用程序玩耍, 学习使用二二。

我创建了一个小型网络应用程序,有两个模型/表格:项目和任务。 (1至多个关系,在模型类中适当配置)。

我现在试图定制任务/ 创建视图, 将文本输入字段替换为一个选择框, 以建议可用的项目列表 。

我打开了形式视图,尝试了这个:

<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>

但它不停地发出警告或错误(例如为foreach () 提供的 < code> Invalid 参数), 并且绝对不起作用。 我无法理解我做错了什么。 您能帮助吗?

最佳回答

你们的论点不合逻辑(应该):

$frameworks = Framework::model()->findAll();
$list = CHtml::listData($frameworks,  id ,  name );
echo $form->listBox($model, framework_id , $list,array());

检查>文件

问题回答

好,我找到了它,多亏了Larry Ullman 出色的辅导

现为:

<?php echo $form->dropDownList($model, project_id , CHtml::listData(Project::model()->findAll(),  id ,  name )); ?>




相关问题
HABTM Relationship and Named Scopes

I ve been working on this for a while now and I cant seem to find where there error. I have a User Model(:name, :password, :email), and Event model(:name, :etc) and Interest model (:name) [>all ...

HABTM Relationships and the Join Table

I m trying to connect the values of two join tables that I have and show the results based on a conditional relationship...and i m having some problems I have a Users Model(:name, :password, :email), ...

do document-oriented databases have integrity?

I m coming from a MySQL background, and I m interested in document-oriented databases, specifically CouchDB. One of the things I m interested in is data integrity. How do document-oriented databases ...

Relational database tables

I m currently working on an ASP.Net MVC project for a software engineering class. My goal is to create a small online game rental system. I currently have a 3 tables, Movies, Games and Registrants; ...

热门标签