1
1
<?php
2
- /**
3
- * @link http://hiqdev.com/yii2-hiart
4
- * @copyright Copyright (c) 2015 HiQDev
5
- * @license http://hiqdev.com/yii2-hiart/license
2
+
3
+ /*
4
+ * Tools to use API as ActiveRecord for Yii2
5
+ *
6
+ * @link https://github.com/hiqdev/yii2-hiart
7
+ * @package yii2-hiart
8
+ * @license BSD-3-Clause
9
+ * @copyright Copyright (c) 2015, HiQDev (https://hiqdev.com/)
6
10
*/
7
11
8
12
namespace hiqdev \hiart ;
9
13
10
- use common \components \Err ;
11
14
use hipanel \base \Re ;
12
15
use yii \db \ActiveQueryInterface ;
13
16
use yii \db \ActiveQueryTrait ;
@@ -24,14 +27,14 @@ class ActiveQuery extends Query implements ActiveQueryInterface
24
27
*/
25
28
const EVENT_INIT = 'init ' ;
26
29
27
-
28
30
/**
29
31
* Constructor.
30
32
*
31
33
* @param array $modelClass the model class associated with this query
32
- * @param array $config configurations to be applied to the newly created query object
34
+ * @param array $config configurations to be applied to the newly created query object
33
35
*/
34
- public function __construct ($ modelClass , $ config = []) {
36
+ public function __construct ($ modelClass , $ config = [])
37
+ {
35
38
$ this ->modelClass = $ modelClass ;
36
39
parent ::__construct ($ config );
37
40
}
@@ -42,7 +45,8 @@ public function __construct ($modelClass, $config = []) {
42
45
* an [[EVENT_INIT]] event. If you override this method, make sure you call the parent implementation at the end
43
46
* to ensure triggering of the event.
44
47
*/
45
- public function init () {
48
+ public function init ()
49
+ {
46
50
parent ::init ();
47
51
$ this ->trigger (self ::EVENT_INIT );
48
52
}
@@ -51,10 +55,12 @@ public function init () {
51
55
* Creates a DB command that can be used to execute this query.
52
56
*
53
57
* @param Connection $db the DB connection used to create the DB command.
54
- * If null, the DB connection returned by [[modelClass]] will be used.
58
+ * If null, the DB connection returned by [[modelClass]] will be used.
59
+ *
55
60
* @return Command the created DB command instance.
56
61
*/
57
- public function createCommand ($ db = null ) {
62
+ public function createCommand ($ db = null )
63
+ {
58
64
if ($ this ->primaryModel !== null ) {
59
65
// lazy loading
60
66
if (is_array ($ this ->via )) {
@@ -89,7 +95,6 @@ public function createCommand ($db = null) {
89
95
$ this ->type = $ modelClass ::type ();
90
96
}
91
97
92
-
93
98
$ commandConfig = $ db ->getQueryBuilder ()->build ($ this );
94
99
95
100
return $ db ->createCommand ($ commandConfig );
@@ -99,10 +104,12 @@ public function createCommand ($db = null) {
99
104
* Executes query and returns all results as an array.
100
105
*
101
106
* @param Connection $db the DB connection used to create the DB command.
102
- * If null, the DB connection returned by [[modelClass]] will be used.
107
+ * If null, the DB connection returned by [[modelClass]] will be used.
108
+ *
103
109
* @return array the query results. If the query results in nothing, an empty array will be returned.
104
110
*/
105
- public function all ($ db = null ) {
111
+ public function all ($ db = null )
112
+ {
106
113
if ($ this ->asArray ) {
107
114
// TODO implement with
108
115
return parent ::all ($ db );
@@ -130,16 +137,18 @@ public function all ($db = null) {
130
137
* Executes query and returns a single row of result.
131
138
*
132
139
* @param Connection $db the DB connection used to create the DB command.
133
- * If null, the DB connection returned by [[modelClass]] will be used.
140
+ * If null, the DB connection returned by [[modelClass]] will be used.
141
+ *
134
142
* @return ActiveRecord|array|null a single row of query result. Depending on the setting of [[asArray]],
135
- * the query result may be either an array or an ActiveRecord object. Null will be returned
136
- * if the query results in nothing.
143
+ * the query result may be either an array or an ActiveRecord object. Null will be returned
144
+ * if the query results in nothing.
137
145
*/
138
- public function one ($ db = null ) {
139
- // $result = $this->createCommand($db)->get();
146
+ public function one ($ db = null )
147
+ {
148
+ // $result = $this->createCommand($db)->get();
140
149
141
150
if (($ result = parent ::one ($ db )) === false ) {
142
- return null ;
151
+ return ;
143
152
}
144
153
if ($ this ->asArray ) {
145
154
// TODO implement with()
@@ -174,9 +183,10 @@ public function one ($db = null) {
174
183
}
175
184
176
185
/**
177
- * @inheritdoc
186
+ * { @inheritdoc}
178
187
*/
179
- public function search ($ db = null , $ options = []) {
188
+ public function search ($ db = null , $ options = [])
189
+ {
180
190
$ result = $ this ->createCommand ($ db )->search ($ options );
181
191
// TODO implement with() for asArray
182
192
if (!empty ($ result ) && !$ this ->asArray ) {
@@ -194,10 +204,11 @@ public function search ($db = null, $options = []) {
194
204
}
195
205
196
206
/**
197
- * @inheritdoc
207
+ * { @inheritdoc}
198
208
*/
199
- public function column ($ field , $ db = null ) {
200
- if ($ field == '_id ' ) {
209
+ public function column ($ field , $ db = null )
210
+ {
211
+ if ($ field === '_id ' ) {
201
212
$ command = $ this ->createCommand ($ db );
202
213
$ command ->queryParts ['fields ' ] = [];
203
214
$ command ->queryParts ['_source ' ] = false ;
@@ -216,7 +227,8 @@ public function column ($field, $db = null) {
216
227
return parent ::column ($ field , $ db );
217
228
}
218
229
219
- public function getList ($ as_array = true , $ db = null , $ options = []) {
230
+ public function getList ($ as_array = true , $ db = null , $ options = [])
231
+ {
220
232
$ rawResult = $ this ->createCommand ($ db )->getList ($ options );
221
233
foreach ($ rawResult as $ k => $ v ) {
222
234
$ result [] = ['gl_key ' => $ k , 'gl_value ' => $ v ];
0 commit comments