From 341b60e301108ed5f0ca78b36086ee05e8631f0e Mon Sep 17 00:00:00 2001 From: Dmytro Karpovych Date: Tue, 11 Aug 2015 22:51:35 +0300 Subject: [PATCH] add EditAction --- README.md | 2 ++ composer.json | 2 +- src/EditAction.php | 56 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 1 deletion(-) create mode 100755 src/EditAction.php diff --git a/README.md b/README.md index b37a47f..a6fd9e0 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,8 @@ Allows save in integer field bit array. Allows get lists from ActiveRecord. ### PasswordTrait Easy work with password. +### EditAction +Action for AJAX record update. ## Installation ```bash diff --git a/composer.json b/composer.json index 1addf26..e4e9b20 100644 --- a/composer.json +++ b/composer.json @@ -28,7 +28,7 @@ "minimum-stability": "dev", "require": { "php": ">=5.4.0", - "yiisoft/yii2": ">=2.0.0" + "yiisoft/yii2": ">=2.0.6" }, "autoload": { "psr-4": { diff --git a/src/EditAction.php b/src/EditAction.php new file mode 100755 index 0000000..72fe2cc --- /dev/null +++ b/src/EditAction.php @@ -0,0 +1,56 @@ +findModel)){ + throw new InvalidConfigException('findModel must be set'); + } + } + + /** + * Set new attribute value and save AR + * @throws NotFoundHttpException + */ + public function run() + { + if (!Yii::$app->request->isAjax) { + throw new NotFoundHttpException('The requested page does not exist.'); + } + $post = Yii::$app->request->post(); + + if (!(empty($post['pk']) || empty($post['name']) || !isset($post['value']))) { + /** @var ActiveRecord $model */ + $model = call_user_func($this->findModel, $post['pk']); + $attribute = $post['name']; + $value = $post['value']; + if (is_callable($this->filter)) { + $value = call_user_func($this->filter, $value, $attribute); + } + $model->$attribute = $value; + $model->save(); + } + } +} \ No newline at end of file