File tree Expand file tree Collapse file tree 3 files changed +59
-1
lines changed Expand file tree Collapse file tree 3 files changed +59
-1
lines changed Original file line number Diff line number Diff line change @@ -12,6 +12,8 @@ Allows save in integer field bit array.
12
12
Allows get lists from ActiveRecord.
13
13
### PasswordTrait
14
14
Easy work with password.
15
+ ### EditAction
16
+ Action for AJAX record update.
15
17
16
18
## Installation
17
19
``` bash
Original file line number Diff line number Diff line change 28
28
"minimum-stability" : " dev" ,
29
29
"require" : {
30
30
"php" : " >=5.4.0" ,
31
- "yiisoft/yii2" : " >=2.0.0 "
31
+ "yiisoft/yii2" : " >=2.0.6 "
32
32
},
33
33
"autoload" : {
34
34
"psr-4" : {
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace nullref \useful ;
4
+
5
+ use Yii ;
6
+ use yii \base \Action ;
7
+ use yii \base \InvalidConfigException ;
8
+ use yii \db \ActiveRecord ;
9
+ use yii \web \NotFoundHttpException ;
10
+
11
+ /**
12
+ * Action for AJAX record update
13
+ */
14
+ class EditAction extends Action
15
+ {
16
+ /** @var callable */
17
+ public $ findModel ;
18
+
19
+ /** @var callable */
20
+ public $ filter ;
21
+
22
+ /**
23
+ * Check if action has valid findModel method
24
+ */
25
+ public function init ()
26
+ {
27
+ parent ::init ();
28
+ if (is_callable ($ this ->findModel )){
29
+ throw new InvalidConfigException ('findModel must be set ' );
30
+ }
31
+ }
32
+
33
+ /**
34
+ * Set new attribute value and save AR
35
+ * @throws NotFoundHttpException
36
+ */
37
+ public function run ()
38
+ {
39
+ if (!Yii::$ app ->request ->isAjax ) {
40
+ throw new NotFoundHttpException ('The requested page does not exist. ' );
41
+ }
42
+ $ post = Yii::$ app ->request ->post ();
43
+
44
+ if (!(empty ($ post ['pk ' ]) || empty ($ post ['name ' ]) || !isset ($ post ['value ' ]))) {
45
+ /** @var ActiveRecord $model */
46
+ $ model = call_user_func ($ this ->findModel , $ post ['pk ' ]);
47
+ $ attribute = $ post ['name ' ];
48
+ $ value = $ post ['value ' ];
49
+ if (is_callable ($ this ->filter )) {
50
+ $ value = call_user_func ($ this ->filter , $ value , $ attribute );
51
+ }
52
+ $ model ->$ attribute = $ value ;
53
+ $ model ->save ();
54
+ }
55
+ }
56
+ }
You can’t perform that action at this time.
0 commit comments