2016-04-19 10 views
0

yii2 select2に存在しない場合は、データベースにカテゴリを追加する方法。in yii2 select2そのオプションが存在しない場合、データベースに値を追加する方法

<?= 
    $form->field($model, 'question_category')->widget(Select2::classname(), [ 
     'data' => ArrayHelper::map(Category::find()->all(),'category_name','category_name'), 
     'maintainOrder' => true, 
     'toggleAllSettings' => [ 
      'selectLabel' => '<i class="glyphicon glyphicon-ok-circle"></i> Tag All', 
      'unselectLabel' => '<i class="glyphicon glyphicon-remove-circle"></i> Untag All', 
      'selectOptions' => ['class' => 'text-success'], 
      'unselectOptions' => ['class' => 'text-danger'], 
     ], 
     'options' => ['multiple' => true, 'placeholder' => 'Select a Category ...'], 
     'pluginOptions' => [ 
      'tags' => true, 
      'maximumInputLength' => 10 
     ], 
    ]); 
?> 

答えて

0

カテゴリのモデルクラスを作成します。次のコードは、モデルの作成に役立ちます。

<?php 
 

 
namespace app\models; 
 

 
use Yii; 
 

 
class Category extends \yii\db\ActiveRecord 
 
{ 
 
    
 
    public static function tableName() 
 
    { 
 
     return 'category'; 
 
    } 
 

 
    
 
    public function rules() 
 
    { 
 
     return [ 
 
      [['category_code', 'category_name'], 'required'], 
 
     ]; 
 
    } 
 

 
    
 
    
 
    public static function find() 
 
    { 
 
     return new CategoryQuery(get_called_class()); 
 
    } 
 
    
 
    
 
}

関連する問題