コード例 #1
0
 public function actionUpdate($id)
 {
     if (!($model = Item::findOne($id))) {
         return $this->redirect(['/admin/' . $this->module->id]);
     }
     if ($model->load(Yii::$app->request->post())) {
         if (Yii::$app->request->isAjax) {
             Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
             return ActiveForm::validate($model);
         } else {
             if (isset($_FILES) && $this->module->settings['articleThumb']) {
                 $model->image = UploadedFile::getInstance($model, 'image');
                 if ($model->image && $model->validate(['image'])) {
                     $model->image = Image::upload($model->image, 'article');
                 } else {
                     $model->image = $model->oldAttributes['image'];
                 }
             }
             if ($model->save()) {
                 foreach ($model->itemCategories as $itemCategory) {
                     $itemCategory->delete();
                 }
                 foreach ($model->categoryIds as $order => $category) {
                     $itemCategory = new ItemCategory(['item_id' => $model->primaryKey, 'category_id' => $category, 'order' => $order]);
                     $itemCategory->save();
                 }
                 $this->flash('success', Yii::t('easyii', 'Article updated'));
                 return $this->redirect(['update', 'id' => $model->primaryKey]);
             } else {
                 $this->flash('error', Yii::t('easyii', 'Update error. {0}', $model->formatErrors()));
                 return $this->refresh();
             }
         }
     } else {
         return $this->render('update', ['model' => $model]);
     }
 }
コード例 #2
0
 public function getItemCategories()
 {
     return $this->hasMany(ItemCategory::className(), ['item_id' => 'item_id']);
 }