public function actionCreate() { $model = new Category(); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['update', 'id' => $model->id]); } else { return $this->render('create', ['model' => $model]); } }
public function search($params) { $query = Category::find(); $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => false, 'sort' => new \yii\data\Sort(['attributes' => ['name']])]); $this->load($params); if (!$this->validate()) { return $dataProvider; } $query->andFilterWhere(['id' => $this->id]); $query->andFilterWhere(['like', 'name', $this->name]); return $dataProvider; }
$this->params['breadcrumbs'][] = $this->title; ?> <div class="field-index"> <h1><?php echo Html::encode($this->title); ?> </h1> <p> <?php echo Html::a('Добавить поле', ['create'], ['class' => 'btn btn-success']); ?> <?php echo Html::a('Категории', ['/field/category'], ['class' => 'btn btn-success']); ?> </p> <?php echo \kartik\grid\GridView::widget(['dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'export' => false, 'columns' => [['class' => 'yii\\grid\\SerialColumn'], 'name', 'slug', ['attribute' => 'category_id', 'label' => 'Категория', 'content' => function ($model) { if ($model->category) { return $model->category->name; } }, 'filter' => Html::activeDropDownList($searchModel, 'type', ArrayHelper::map(Category::find()->all(), 'id', 'name'), ['class' => 'form-control', 'prompt' => 'Категория'])], ['attribute' => 'relation_model', 'content' => function ($model) { return @yii::$app->getModule('field')->relationModels[$model->relation_model]; }, 'filter' => Html::activeDropDownList($searchModel, 'type', yii::$app->getModule('field')->relationModels, ['class' => 'form-control', 'prompt' => 'Модель'])], ['attribute' => 'type', 'content' => function ($model) { return yii::$app->getModule('field')->types[$model->type]; }, 'filter' => Html::activeDropDownList($searchModel, 'type', yii::$app->getModule('field')->types, ['class' => 'form-control', 'prompt' => 'Тип'])], 'description', ['class' => 'yii\\grid\\ActionColumn', 'template' => '{update} {delete}', 'buttonOptions' => ['class' => 'btn btn-default'], 'options' => ['style' => 'width: 110px;']]]]); ?> </div>
public function getCategory() { return $this->hasOne(Category::className(), ['id' => 'category_id']); }
*/ use yii\helpers\Html; use yii\helpers\ArrayHelper; use yii\widgets\ActiveForm; use app\modules\field\models\Category; ?> <div class="field-form"> <?php $form = ActiveForm::begin(); ?> <div class="row"> <div class="col-lg-6"> <?php echo $form->field($model, 'category_id')->dropdownList(ArrayHelper::map(Category::find()->all(), 'id', 'name')); ?> <?php echo $form->field($model, 'name')->textInput(); ?> <?php echo $form->field($model, 'slug')->textInput(); ?> </div> <div class="col-lg-6"> <?php echo $form->field($model, 'relation_model')->dropdownList(yii::$app->getModule('field')->relationModels); ?> <?php echo $form->field($model, 'type')->dropdownList(Yii::$app->getModule('field')->types); ?>