从相关表中的Yii创建下拉列表 [英] Creating dropdown list from related table yii
本文介绍了从相关表中的Yii创建下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个表:产品和产品类型。
I have two tables: product and product_type.
product : product_id, product_type_id, name, etc...
product_type : product_type_id, name, desc, etc...
两者都与键product_type_id。
Both are related with key "product_type_id".
我已经创建了污物使用GII CRUD生成两个表。现在,在产品形态页我要显示所有产品类型名称的下拉领域使用Yii ActiveRecord的上市。我曾在_form页加入这一行:
I have created the crud for both tables using gii crud generator. Now on Product form page I want to display the listing of all product_type name in dropdown field using Yii ActiveRecord. I have added this line in _form page:
<?php
$list = CHtml::listData(ProductType::model()->findAll(array('order' => 'product_type_name')), 'id', 'id');
echo $form->dropDownList($model, 'product_type_id', $list);
?>
但它显示空白下拉领域:(
But it's showing blank dropdown field :(
我怎样才能做到这一点?
How can I do this?
推荐答案
解决自己:)
通过只是提供product_type_name。
By just providing product_type_name.
<?php
$list = CHtml::listData(ProductType::model()->findAll(array('order' => 'product_type_name')), 'product_type_id', 'product_type_name');
echo $form->dropDownList($model, 'product_type_id', $list);
?>
这篇关于从相关表中的Yii创建下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文