Exemple #1
0
 public function save()
 {
     $model = new Announcement();
     if ($this->id) {
         $model = Announcement::findOne($this->id);
     }
     foreach ($this->attributes as $attribute => $value) {
         if (array_key_exists($attribute, $model->attributes)) {
             $model->{$attribute} = $value;
         }
     }
     $model->categories = $this->categories;
     $model->articles = $this->articles;
     $model->params = ['titleLength' => $this->titleLength, 'textLength' => $this->textLength];
     return $model->save();
 }
 public function actionToggle()
 {
     if (!\Yii::$app->request->isAjax) {
         throw new BadRequestHttpException("Данный запрос возможен только через ajax!");
     }
     if (!($attribute = \Yii::$app->request->post("attribute"))) {
         throw new InvalidParamException("Не передан attribute!");
     }
     if (!($key = \Yii::$app->request->post("key"))) {
         throw new InvalidParamException("Не передан key!");
     }
     if (!($value = \Yii::$app->request->post("value"))) {
         throw new InvalidParamException("Не передан value!");
     } else {
         $value = $value == 'true' ? true : false;
     }
     $model = Announcement::findOne($key);
     if (!$model) {
         throw new NotFoundHttpException("Не найдены новости с передаными идентификаторами!");
     }
     $model->setAttribute($attribute, $value);
     return $model->save(false);
 }