Yii2 下拉不同的 Activerecord
Yii2 dropdown different Activerecord
我正在创建一个表单,用于将具有 activeDropdownList 的产品输入到来自不同 table 的 select 产品类别。
我在产品和类别模型中使用 ActiveRecord class,并使用类别模型来填充下拉列表。
当我尝试插入产品时,它失败了,因为下拉列表的名称。
所有其他字段的名称类似于 name="CreateEvent[tanggal]">
,而我的类别下拉列表:name="id">
如何在不硬编码下拉属性的情况下集成它?
在我的控制器上:
$kategori = $items = ArrayHelper::map(Kategori::find()->all(), 'id', 'nama');
// check post header, call $model->save() if post is exist
if($model->load(Yii::$app->request->post()) && $model->save()) {
Yii::$app->session->setFlash('status', 'success');
return $this->redirect(['create']);
}
我的看法:
$form = ActiveForm::begin();
echo $form->field($model, 'nama')->input('text');
echo $form->field($model, 'lokasi')->input('text');
echo $form->field($model, 'tanggal')->widget(DatePicker:: classname(),
[
]);
echo Html::activeDropDownList($model,'id',$kategori,[]);
echo Html::submitButton('Submit', ['class'=>'btn btn-primary']);
ActiveForm::end();
谢谢。
您 table 中的字段类似于 'category_id'。
因此您的下拉代码应该是
echo Html::activeDropDownList($model,'category_id',$kategori,[]);
如果我没理解错的话,
您在保存类别的产品 table 中的列名是什么?
而不是使用 HTML 你为什么不使用这样的东西:
use yii\helpers\ArrayHelper;
$form->field($model,'your column name')->dropdownlist(ArrayHelper::map(Kategori::find()->all(), 'id', 'nama'));
我正在创建一个表单,用于将具有 activeDropdownList 的产品输入到来自不同 table 的 select 产品类别。 我在产品和类别模型中使用 ActiveRecord class,并使用类别模型来填充下拉列表。 当我尝试插入产品时,它失败了,因为下拉列表的名称。
所有其他字段的名称类似于 name="CreateEvent[tanggal]">
,而我的类别下拉列表:name="id">
如何在不硬编码下拉属性的情况下集成它?
在我的控制器上:
$kategori = $items = ArrayHelper::map(Kategori::find()->all(), 'id', 'nama');
// check post header, call $model->save() if post is exist
if($model->load(Yii::$app->request->post()) && $model->save()) {
Yii::$app->session->setFlash('status', 'success');
return $this->redirect(['create']);
}
我的看法:
$form = ActiveForm::begin();
echo $form->field($model, 'nama')->input('text');
echo $form->field($model, 'lokasi')->input('text');
echo $form->field($model, 'tanggal')->widget(DatePicker:: classname(),
[
]);
echo Html::activeDropDownList($model,'id',$kategori,[]);
echo Html::submitButton('Submit', ['class'=>'btn btn-primary']);
ActiveForm::end();
谢谢。
您 table 中的字段类似于 'category_id'。
因此您的下拉代码应该是
echo Html::activeDropDownList($model,'category_id',$kategori,[]);
如果我没理解错的话, 您在保存类别的产品 table 中的列名是什么?
而不是使用 HTML 你为什么不使用这样的东西:
use yii\helpers\ArrayHelper;
$form->field($model,'your column name')->dropdownlist(ArrayHelper::map(Kategori::find()->all(), 'id', 'nama'));