Im new to yii2 and im working on an already build website and im trying to add new fields to the form, its all working upon 1 field, this field is a checkbox list, which wont save anything to the DB.
view.php
$form->field($model, 'sort_abc')->checkBoxLIst( ['a' => 'Item A', 'b' => 'Item B', 'c' => 'Item C'])->label($model->getAttributeLabel('sort_abc'));
models/filter.php
<?php
namespace common\models;
use Yii;
use yii\db\Query;
use common\components\ActiveStateBehavior;
use yii\behaviors\TimestampBehavior;
/**
* This is the model class for table "{{%filter}}".
*
* @property integer $id
* @property integer $is_enabled
* @property integer $is_deleted
* @property string $description
* @property integer $created_at
* @property integer $update_at
*/
class Filter extends \yii\db\ActiveRecord
{
const STATUS_ACTIVE = 1;
const STATUS_INACTIVE = 0;
/**
* @inheritdoc
*/
public static function tableName()
{
return '{{%filter}}';
}
/**
* @inheritdoc
*/
public function behaviors()
{
return [
ActiveStateBehavior::className(),
[
'class' => 'nzz\translations\ActiveTranslationBehavior',
'attributes' => ['name'],
'languages' => Yii::$app->params['languages'],
'relation' => 'translations',
],
TimestampBehavior::className(),
];
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['name', 'description'], 'required'],
[['is_enabled', 'is_deleted', 'created_at', 'updated_at', 'visible_on_portal', 'is_blocked'], 'integer'],
[['global_name' ], 'string'],
[['sort_abc' ], 'string'],
[['sort_index'], 'safe'],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => Yii::t('app', 'ID'),
'name' => Yii::t('app', 'Name'),
'description' => Yii::t('app', 'Description'),
'is_enabled' => Yii::t('app', 'Active'),
'is_deleted' => Yii::t('app', 'Deleted'),
'visible_on_portal' => Yii::t('app', 'visibleOnPortal'),
'is_blocked' => Yii::t('app', 'Blocked'),
'global_name' => Yii::t('app', 'filterGlobalName'),
'sort_index' => Yii::t('app', 'sortIndex'),
'sort_abc' => Yii::t('app', 'sortAbc'),
];
}
}
}
controllers/filtercontroller.php
<?php
namespace backend\controllers;
use Yii;
use yii\web\Controller;
use backend\models\FilterSearch;
use common\models\Filter;
class FilterController extends Controller{
public function behaviors()
{
return [
'access' => [
'class' => \yii\filters\AccessControl::className(),
'rules' => [
[
'allow' => true,
'actions' => ['index'],
'roles' => ['filterRead'],
],
[
'allow' => true,
'actions' => ['view'],
'roles' => ['filterWrite'],
],
[
'allow' => true,
'actions' => ['delete'],
'roles' => ['filterDelete'],
],
]
],
];
}
public function actionIndex(){
$searchModel = new FilterSearch;
$searchModel->load($_GET);
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
public function actionView($id = null){
$model = new Filter();
if(!empty($id)){
$model = Filter::findOne(['id'=>$id]);
}
if(Yii::$app->request->isPost){
$post = Yii::$app->request->post();
if($model->load($post) && $model->save()){
if(isset($post['redirect']) && !empty($post['redirect'])){
return $this->redirect($post['redirect']);
}
Yii::$app->end();
}
}
return $this->renderAjax('view',['model'=>$model]);
}
}
?>