Creating dropdown list from related table yii(从相关表 yii 创建下拉列表)
问题描述
我有两个表: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);
?>
这篇关于从相关表 yii 创建下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从相关表 yii 创建下拉列表


基础教程推荐
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- PHP 守护进程/worker 环境 2022-01-01
- 使用 PDO 转义列名 2021-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- HTTP 与 FTP 上传 2021-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01