public static function edit($id, CategoryForm $form) { $category = Category::findOne($id); if (!$category) { return false; } $category->name = $form->name; $category->status = $form->status; return $category->save(); }
public static function categories() { $categories = Category::find(['status' => Category::STATUS_NORMAL])->asArray()->select('cid,name')->all(); $options = ['' => '请选择']; if (!$categories) { return $options; } foreach ($categories as $row) { $options[$row['cid']] = $row['name']; } return $options; }
public function actionEdit($id) { $model = new CategoryForm(['scenario' => 'add']); if ($model->load(\Yii::$app->request->post()) and $model->edit($id)) { $this->success(); $this->redirect('/category/list'); } $category = Category::findOne($id); !$category and $this->error('参数错误'); $model->name = $category->name; $model->status = $category->status; return $this->render('add', ['model' => $model]); }
<h5 class="heading">添加菜单分类</h5> <hr/> <?php use app\models\sysadm\Category; use yii\widgets\ActiveForm; use yii\helpers\Html; $form = ActiveForm::begin(['options' => ['class' => 'form-horizontal'], 'fieldConfig' => ['labelOptions' => ['class' => 'col-lg-2 control-label'], 'template' => '{label}<div class="col-lg-3">{input}</div><div class="col-lg-2">{error}</div>']]); echo $form->field($model, 'name'); echo $form->field($model, 'status', ['template' => '{label}<div class="col-lg-3 radio">{input}</div><div class="col-lg-2">{error}</div>'])->radioList(Category::status()); ?> <div class="form-group"> <div class="col-lg-2"></div> <div class="col-lg-1"> <?php echo Html::submitButton('提交', ['class' => 'btn btn-primary btn-block', 'name' => 'login-button']); ?> </div> </div> <?php ActiveForm::end();
public function edit($id) { if ($this->validate()) { return Category::edit($id, $this); } }