Yii 2.0从数据库中选择预选值
我一直在试图解决一个问题,但没有结果,但我相信我会找到一个解决办法。我正在使用Kartik2.0Select扩展来进行多重选择。很好,在插入数据库时所有操作都正常,但我无法检索保存的记录,以便在“选择”字段中显示为“已选择” //我已经包括了kartik小部件 使用kartik\widgets\Select2Yii 2.0从数据库中选择预选值,yii,Yii,我一直在试图解决一个问题,但没有结果,但我相信我会找到一个解决办法。我正在使用Kartik2.0Select扩展来进行多重选择。很好,在插入数据库时所有操作都正常,但我无法检索保存的记录,以便在“选择”字段中显示为“已选择” //我已经包括了kartik小部件 使用kartik\widgets\Select2 <label>Desired Specialization(s)</label> <?= $form->field($spec,
<label>Desired Specialization(s)</label>
<?= $form->field($spec, 'id')->label(false)->widget(Select2::classname(), [
'data' => $model->getAllSpecializations(),
'options' => ['placeholder' => 'You can choose more than one specialization ...'],
'pluginOptions' => [
'allowClear' => true,
'multiple' => true
],
]);
?>
</div>
<?php
//the line below is to fetch the array key of $model->getCandidateLanguage() array
$lang->id = array_keys($model->getCandidateLanguage()); // value to initialize
echo Select2::widget([
'model' => $lang,
'attribute' => 'id',
'data' => $model->getAllLanguages(),
'options' => ['placeholder' => 'Choose multiple languages'],
'pluginOptions' => [
'allowClear' => true,
'multiple' => true,
'tags' => true,
],
]);
?>
所需专业化
请回复,我们将不胜感激。谢谢我想您需要添加保存的值作为初始数据吗?像这样:
'value' => $savedDataArray, // initial value
在深入研究代码之后,我找到了一种方法,可以使用Yii Select2将选定的数据库值显示到多选选项中 我的型号
public function getCandidateLanguage()
{
$langValues = (new \yii\db\Query())
->select('c.language_id AS id, l.lang_name')
->from('candidate_language c ')
->innerJoin('languages l','c.language_id = l.id')
->where('c.candidate_id='.$this->candidate_id)
->orderBy('c.language_id')
->all();
return \yii\helpers\ArrayHelper::map($langValues,'id','lang_name');
}
我的观点
使用kartik\widgets\Select2
<label>Desired Specialization(s)</label>
<?= $form->field($spec, 'id')->label(false)->widget(Select2::classname(), [
'data' => $model->getAllSpecializations(),
'options' => ['placeholder' => 'You can choose more than one specialization ...'],
'pluginOptions' => [
'allowClear' => true,
'multiple' => true
],
]);
?>
</div>
<?php
//the line below is to fetch the array key of $model->getCandidateLanguage() array
$lang->id = array_keys($model->getCandidateLanguage()); // value to initialize
echo Select2::widget([
'model' => $lang,
'attribute' => 'id',
'data' => $model->getAllLanguages(),
'options' => ['placeholder' => 'Choose multiple languages'],
'pluginOptions' => [
'allowClear' => true,
'multiple' => true,
'tags' => true,
],
]);
?>
希望它能帮助面临同样问题的人。展示您的型号(专业化)和控制器