我不是在Yii2的dropDownList中抓取選定的值($ model-> attribute),可能會出錯?由於如何獲取Yii2中dropDownList的值?
這是位於上查看代碼:
$command = $connection->createCommand('SELECT att1
FROM table
ORDER By table_id ASC');
$rows = $command->queryAll();
$command->execute();
$listData = ArrayHelper::index($rows, null, 'table_id');
然後在同一查看我打電話$的ListData
<div class="row">
<div class="col-lg-9">
<?php Pjax::begin(['enablePushState' => false]); ?>
<?php $form = ActiveForm::begin(['id' => 'test-form', 'options' => ['data-pjax' => true]
]); ?>
<?= $form->field($model, 'attribute')->dropDownList($listData, ['prompt'=>'List of attributes.']); ?>
<div class="form-group">
<?= Html::submitButton('Submit', ['class' => 'btn btn-primary', 'name' => 'test-button']) ?>
</div>
<?php ActiveForm::end(); ?>
<?php Pjax::end(); ?>
</div>
</div>
這是控制器:
public function actionTest()
{
$model = new TestForm();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
$model->insertTest();
return $this->renderAjax('test', ['model' => $model]);
} else {
return $this->renderAjax('test', ['model' => $model]);
}
}
該模型具有$屬性的定義,insertTest()是一個函數,它使用$屬性值que ry到數據庫。因爲你需要一個一維數組
您需要向我們展示如何啓動'$ model'。可能在控制器中並從POST/GET請求中加載..? – lubosdz