2015-10-07 31 views
0

我一直在试图解决一个问题,但无济于事,但我相信我会在这里找到解决方案。我正在使用Kartik 2.0选择扩展来执行多选。很好,在插入数据库时​​所有工作都正常,但我无法检索保存的记录,以便在选择字段中选择显示。Yii 2.0从数据库中选择预选值

//我已经包括卡尔蒂克部件已经

使用卡尔蒂克\小工具\选择二;

<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> 

请您的答复将不胜感激。谢谢

+0

显示你的模型(专业化)和你的控制器 – scaisEdge

回答

0

经过一番挖掘到代码中,我发现如何使用Yii选择二

我的模型显示选择的数据库值成多选择选项上的方式

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;

<?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, 
       ], 

      ]); 

     ?> 

希望它能帮助面临同样问题的人。