13
13
use yii \base \InvalidConfigException ;
14
14
use yii \data \ActiveDataProvider ;
15
15
use yii \db \ActiveQuery ;
16
+ use yii \db \ActiveRecord ;
16
17
use yii \web \Response ;
17
18
18
19
/**
23
24
class DataTableAction extends Action
24
25
{
25
26
/**
26
- * GET or POST
27
- *
27
+ * Types of request method
28
+ */
29
+ const REQUEST_METHOD_GET = 'GET ' ;
30
+ const REQUEST_METHOD_POST = 'POST ' ;
31
+
32
+ /**
33
+ * @see \nullref\datatable\DataTableAction::getParam
28
34
* @var string
29
35
*/
30
- public $ requestMethod = " GET " ;
36
+ public $ requestMethod = self :: REQUEST_METHOD_GET ;
31
37
32
38
/**
33
39
* @var ActiveQuery
@@ -66,20 +72,20 @@ class DataTableAction extends Action
66
72
*/
67
73
public $ formatResponse ;
68
74
75
+ /**
76
+ * Check if query is configured
77
+ * @throws InvalidConfigException
78
+ */
69
79
public function init ()
70
80
{
71
81
if ($ this ->query === null ) {
72
82
throw new InvalidConfigException (get_class ($ this ) . '::$query must be set. ' );
73
83
}
74
84
}
75
85
76
- protected function getParam ($ name , $ defaultValue = null )
77
- {
78
- return $ this ->requestMethod == 'GET ' ?
79
- Yii::$ app ->request ->getQueryParam ($ name , $ defaultValue ) :
80
- Yii::$ app ->request ->getBodyParam ($ name , $ defaultValue );
81
- }
82
-
86
+ /**
87
+ * @return array|ActiveRecord[]
88
+ */
83
89
public function run ()
84
90
{
85
91
/** @var ActiveQuery $originalQuery */
@@ -114,6 +120,19 @@ public function run()
114
120
return $ this ->formatResponse ($ response );
115
121
}
116
122
123
+ /**
124
+ * Extract param from request
125
+ * @param $name
126
+ * @param null $defaultValue
127
+ * @return mixed
128
+ */
129
+ protected function getParam ($ name , $ defaultValue = null )
130
+ {
131
+ return $ this ->requestMethod == self ::REQUEST_METHOD_GET ?
132
+ Yii::$ app ->request ->getQueryParam ($ name , $ defaultValue ) :
133
+ Yii::$ app ->request ->getBodyParam ($ name , $ defaultValue );
134
+ }
135
+
117
136
/**
118
137
* @param ActiveQuery $query
119
138
* @param array $columns
@@ -164,7 +183,7 @@ public function applyOrder(ActiveQuery $query, $columns, $order)
164
183
/**
165
184
* @param ActiveQuery $query
166
185
* @param array $columns
167
- * @return ActiveQuery
186
+ * @return array|ActiveRecord[]
168
187
*/
169
188
public function formatData (ActiveQuery $ query , $ columns )
170
189
{
@@ -177,7 +196,7 @@ public function formatData(ActiveQuery $query, $columns)
177
196
178
197
/**
179
198
* @param array $response
180
- * @return ActiveQuery
199
+ * @return array|ActiveRecord[]
181
200
*/
182
201
public function formatResponse ($ response )
183
202
{
0 commit comments