我想列出我的模型的所有對象,並寫入文件所選模型的id。使用SiteController我呈現我的頁面,但我將使用什麼模型?其他控制器在yii中的下拉列表
$models = myModel::model()->findAll();
$list = CHtml::listData($models, 'id', 'name');
echo CHtml::dropDownList(???? , $select, $list);
我想列出我的模型的所有對象,並寫入文件所選模型的id。使用SiteController我呈現我的頁面,但我將使用什麼模型?其他控制器在yii中的下拉列表
$models = myModel::model()->findAll();
$list = CHtml::listData($models, 'id', 'name');
echo CHtml::dropDownList(???? , $select, $list);
如果我得到你想要做的,你在談論兩種模型。像tbl_product => Product和tbl_category => Category。
爲了演示的目的:說,你想創建一個新產品,每個產品必須屬於一個類別,那麼你可以利用活動下拉菜單。使用類似於你的代碼,你可以說:
$category = Category::model()->findAll();
$list = CHtml::listData($category, 'id', 'name');
要注意的重要一點是,了CHtml :: activeDropDownList()預計不同類型的參數。它和cHTML ::下拉列表()之間的主要區別是,activeDropDownList(綁定到一個模型,而下拉列表()不是。
public static string activeDropDownList(CModel $model, string $attribute, array $data, array $htmlOptions=array())
public static string dropDownList(string $name, string $select, array $data, array $htmlOptions=array())
因此,使用的例子,假設我們的產品型號有一個叫場
CHtml::activeDropDownList($model, 'category_id', $list);
,或者如果你已經創建了一個對象的ActiveForm像這樣:
$form=$this->beginWidget('CActiveForm');
那麼你可以創建CATEGORY_ID,然後將使用既可以產生下拉列表像這樣的dropDown列表:
$form->dropDownList($model, 'category_id', $list);
其中$模型將是產品模型。
我希望這有幫助。
Yii,是的。謝謝! –
不客氣。任何時候 –
當你說列出所有對象時,你是什麼意思?我猜你的模型有多個數據,那麼你想在你的下拉菜單中顯示什麼?你是否遇到特定的錯誤? dropDownList的第一個參數只是一個字符串,應該是在生成的HTML中用於輸入字段的名稱。無論如何,這個問題在任何事情能夠得到解答之前都需要大量澄清。 – ernie
對不起,CHtml :: activeDropDownList –
我想顯示名稱,但我也想寫所選模型的'id'到變量 –