データベースからリストを取得し、productTypes
それらをドロップダウンリストに出力して、2番目のドロップダウンリストにAjaxで入力するために使用したいと考えています。
ステップ 1: コントローラーがproductTypes
データベースから取得する ステップ 2: コントローラーがproductTypes
オブジェクトをリストに変換する ステップ 3a: リストを出力してビューでテストする ステップ 3b: ドロップダウンにリスト ビューを入力する
コントローラーのスニペット
public function init()
{
$this->dynamicTypes();
$this->render('calculator', array('types' => $this->_types));
}
public function dynamicTypes()
{
$this->_types = CHtml::listData(ProductType::model()->findAll(), 'id', 'type');
}
ファイル スニペットを表示
<?php
// Step 3a (this works fine)
echo '<pre>';
print_r($types);
echo '</pre>';
// Step 3b - Returns an error
echo $form->dropDownList('productTypes',1, array($types));
?>
<?php $this->endWidget(); ?>
</div>
ステップ 3b では、次のことを試しました。
echo $form->dropDownList('productTypes',1, array($types));
Error Msg: get_class() expects parameter 1 to be object, string given
http://www.yiiframework.com/doc/api/1.1/CHtml#dropDownList-detailによると、dropDownList メソッドは次の引数を取ります。
public static string dropDownList(string $name, string $select, array $data, array $htmlOptions=array ( ))
最初の引数は、入力フィールドの名前を示す文字列です。
私は何を間違えましたか?どうすれば修正できますか?
アップデート
echo CHtml::dropDownList('productTypes',1, array($types));
動作しているように見えますが、ビューのドロップダウンを見ると、何らかの理由でドロップダウンリストに 0 があります。何らかの理由で、オプションをオプショングループに入れています
<select name="productTypes" id="productTypes">
<optgroup label="0">
<option value="1">Scrap</option>
<option value="2">Coin</option>
<option value="3">Bar</option>
</optgroup>
</select>
解決済み: を削除array($types)
し、単に$types
echo CHtml::dropDownList('productTypes',1, $types);