从相关表 yii 创建下拉列表 [英] Creating dropdown list from related table yii

查看:23
本文介绍了从相关表 yii 创建下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表:productproduct_type(与模型相关,分别是 Product 和 Product_type):

I have two tables: product and product_type (related to the models resp. 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 生成器为两个表创建了 crud.现在在产品表单页面上,我想使用 Yii ActiveRecord 在下拉字段中显示所有 product_type 名称的列表.我在 views/product/_form.php 脚本中添加了这一行:

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 views/product/_form.php script:

<?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 :(

我该怎么做?

推荐答案

Solved MySelf :)

Solved MySelf :)

只需提供 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆