|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @link http://www.yiiframework.com/ |
|
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
|
5
|
|
|
* @license http://www.yiiframework.com/license/ |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace yii\rest; |
|
9
|
|
|
|
|
10
|
|
|
use Yii; |
|
11
|
|
|
use yii\data\ActiveDataProvider; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* IndexAction implements the API endpoint for listing multiple models. |
|
15
|
|
|
* |
|
16
|
|
|
* For more details and usage information on IndexAction, see the [guide article on rest controllers](guide:rest-controllers). |
|
17
|
|
|
* |
|
18
|
|
|
* @author Qiang Xue <[email protected]> |
|
19
|
|
|
* @since 2.0 |
|
20
|
|
|
*/ |
|
21
|
|
|
class IndexAction extends Action |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var callable a PHP callable that will be called to prepare a data provider that |
|
25
|
|
|
* should return a collection of the models. If not set, [[prepareDataProvider()]] will be used instead. |
|
26
|
|
|
* The signature of the callable should be: |
|
27
|
|
|
* |
|
28
|
|
|
* ```php |
|
29
|
|
|
* function (IndexAction $action) { |
|
30
|
|
|
* // $action is the action object currently running |
|
31
|
|
|
* } |
|
32
|
|
|
* ``` |
|
33
|
|
|
* |
|
34
|
|
|
* The callable should return an instance of [[ActiveDataProvider]]. |
|
35
|
|
|
* |
|
36
|
|
|
* If [[dataFilter]] is set the result of [[DataFilter::build()]] will be passed to the callable as a second parameter. |
|
37
|
|
|
* In this case the signature of the callable should be the following: |
|
38
|
|
|
* |
|
39
|
|
|
* ```php |
|
40
|
|
|
* function (IndexAction $action, mixed $filter) { |
|
41
|
|
|
* // $action is the action object currently running |
|
42
|
|
|
* // $filter the built filter condition |
|
43
|
|
|
* } |
|
44
|
|
|
* ``` |
|
45
|
|
|
*/ |
|
46
|
|
|
public $prepareDataProvider; |
|
47
|
|
|
/** |
|
48
|
|
|
* @var DataFilter|null data filter to be used for the search filter composition. |
|
49
|
|
|
* You must setup this field explicitly in order to enable filter processing. |
|
50
|
|
|
* For example: |
|
51
|
|
|
* |
|
52
|
|
|
* ```php |
|
53
|
|
|
* [ |
|
54
|
|
|
* 'class' => 'yii\rest\ActiveDataFilter', |
|
55
|
|
|
* 'searchModel' => function () { |
|
56
|
|
|
* return (new \yii\base\DynamicModel(['id' => null, 'name' => null, 'price' => null])) |
|
57
|
|
|
* ->addRule('id', 'integer') |
|
58
|
|
|
* ->addRule('name', 'trim') |
|
59
|
|
|
* ->addRule('name', 'string') |
|
60
|
|
|
* ->addRule('price', 'number'); |
|
61
|
|
|
* }, |
|
62
|
|
|
* ] |
|
63
|
|
|
* ``` |
|
64
|
|
|
* |
|
65
|
|
|
* @see DataFilter |
|
66
|
|
|
* |
|
67
|
|
|
* @since 2.0.13 |
|
68
|
|
|
*/ |
|
69
|
|
|
public $dataFilter; |
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @return ActiveDataProvider |
|
74
|
|
|
*/ |
|
75
|
|
|
public function run() |
|
76
|
|
|
{ |
|
77
|
|
|
if ($this->checkAccess) { |
|
78
|
|
|
call_user_func($this->checkAccess, $this->id); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return $this->prepareDataProvider(); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Prepares the data provider that should return the requested collection of the models. |
|
86
|
|
|
* @return ActiveDataProvider |
|
87
|
|
|
*/ |
|
88
|
|
|
protected function prepareDataProvider() |
|
89
|
|
|
{ |
|
90
|
|
|
$requestParams = Yii::$app->getRequest()->getBodyParams(); |
|
91
|
|
|
if (empty($requestParams)) { |
|
92
|
|
|
$requestParams = Yii::$app->getRequest()->getQueryParams(); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$filter = null; |
|
96
|
|
|
if ($this->dataFilter !== null) { |
|
97
|
|
|
$this->dataFilter = Yii::createObject($this->dataFilter); |
|
|
|
|
|
|
98
|
|
|
if ($this->dataFilter->load($requestParams)) { |
|
99
|
|
|
$filter = $this->dataFilter->build(); |
|
100
|
|
|
if ($filter === false) { |
|
101
|
|
|
return $this->dataFilter; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
if ($this->prepareDataProvider !== null) { |
|
107
|
|
|
return call_user_func($this->prepareDataProvider, $this, $filter); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/* @var $modelClass \yii\db\BaseActiveRecord */ |
|
111
|
|
|
$modelClass = $this->modelClass; |
|
112
|
|
|
|
|
113
|
|
|
$query = $modelClass::find(); |
|
114
|
|
|
if (!empty($filter)) { |
|
115
|
|
|
$query->andWhere($filter); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return Yii::createObject([ |
|
119
|
|
|
'class' => ActiveDataProvider::className(), |
|
120
|
|
|
'query' => $query, |
|
121
|
|
|
'pagination' => [ |
|
122
|
|
|
'params' => $requestParams |
|
123
|
|
|
], |
|
124
|
|
|
'sort' => [ |
|
125
|
|
|
'params' => $requestParams |
|
126
|
|
|
], |
|
127
|
|
|
]); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: