Mark Mark - 6 months ago 40
PHP Question

Creating dropdown list from related table yii

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


Both are related with key "product_type_id".

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

How can I do this?

Answer

Solved MySelf :)

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);
        ?>