Пример #1
0
 public function actionCreate($id)
 {
     if (!($category = Category::findOne($id))) {
         return $this->redirect(['/populac/' . $this->module->id]);
     }
     $model = new Item();
     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 {
             $model->category_id = $category->primaryKey;
             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 = '';
                 }
             }
             if ($model->save()) {
                 $this->flash('success', Yii::t('easyii/article', 'Article created'));
                 return $this->redirect(['/populac/' . $this->module->id . '/items/edit', 'id' => $model->primaryKey]);
             } else {
                 $this->flash('error', Yii::t('easyii', 'Create error. {0}', $model->formatErrors()));
                 return $this->refresh();
             }
         }
     } else {
         return $this->render('create', ['model' => $model, 'category' => $category]);
     }
 }
Пример #2
0
 private function findCategory($id_slug)
 {
     $category = Category::find()->where(['or', 'category_id=:id_slug', 'slug=:id_slug'], [':id_slug' => $id_slug])->status(Item::STATUS_ON)->one();
     return $category ? new CategoryObject($category) : null;
 }
Пример #3
0
 public function getCategory()
 {
     return $this->hasOne(Category::className(), ['category_id' => 'category_id']);
 }