从相关表 yii 创建下拉列表

2022-01-04 00:00:00 php yii2 yii activerecord

我有两个表:product 和 product_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);
        ?>

相关文章