11<?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/)
610 */
711
812namespace hiqdev \hiart ;
913
10- use common \components \Err ;
1114use hipanel \base \Re ;
1215use yii \db \ActiveQueryInterface ;
1316use yii \db \ActiveQueryTrait ;
@@ -24,14 +27,14 @@ class ActiveQuery extends Query implements ActiveQueryInterface
2427 */
2528 const EVENT_INIT = 'init ' ;
2629
27-
2830 /**
2931 * Constructor.
3032 *
3133 * @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
3335 */
34- public function __construct ($ modelClass , $ config = []) {
36+ public function __construct ($ modelClass , $ config = [])
37+ {
3538 $ this ->modelClass = $ modelClass ;
3639 parent ::__construct ($ config );
3740 }
@@ -42,7 +45,8 @@ public function __construct ($modelClass, $config = []) {
4245 * an [[EVENT_INIT]] event. If you override this method, make sure you call the parent implementation at the end
4346 * to ensure triggering of the event.
4447 */
45- public function init () {
48+ public function init ()
49+ {
4650 parent ::init ();
4751 $ this ->trigger (self ::EVENT_INIT );
4852 }
@@ -51,10 +55,12 @@ public function init () {
5155 * Creates a DB command that can be used to execute this query.
5256 *
5357 * @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+ *
5560 * @return Command the created DB command instance.
5661 */
57- public function createCommand ($ db = null ) {
62+ public function createCommand ($ db = null )
63+ {
5864 if ($ this ->primaryModel !== null ) {
5965 // lazy loading
6066 if (is_array ($ this ->via )) {
@@ -89,7 +95,6 @@ public function createCommand ($db = null) {
8995 $ this ->type = $ modelClass ::type ();
9096 }
9197
92-
9398 $ commandConfig = $ db ->getQueryBuilder ()->build ($ this );
9499
95100 return $ db ->createCommand ($ commandConfig );
@@ -99,10 +104,12 @@ public function createCommand ($db = null) {
99104 * Executes query and returns all results as an array.
100105 *
101106 * @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+ *
103109 * @return array the query results. If the query results in nothing, an empty array will be returned.
104110 */
105- public function all ($ db = null ) {
111+ public function all ($ db = null )
112+ {
106113 if ($ this ->asArray ) {
107114 // TODO implement with
108115 return parent ::all ($ db );
@@ -130,16 +137,18 @@ public function all ($db = null) {
130137 * Executes query and returns a single row of result.
131138 *
132139 * @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+ *
134142 * @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.
137145 */
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();
140149
141150 if (($ result = parent ::one ($ db )) === false ) {
142- return null ;
151+ return ;
143152 }
144153 if ($ this ->asArray ) {
145154 // TODO implement with()
@@ -174,9 +183,10 @@ public function one ($db = null) {
174183 }
175184
176185 /**
177- * @inheritdoc
186+ * { @inheritdoc}
178187 */
179- public function search ($ db = null , $ options = []) {
188+ public function search ($ db = null , $ options = [])
189+ {
180190 $ result = $ this ->createCommand ($ db )->search ($ options );
181191 // TODO implement with() for asArray
182192 if (!empty ($ result ) && !$ this ->asArray ) {
@@ -194,10 +204,11 @@ public function search ($db = null, $options = []) {
194204 }
195205
196206 /**
197- * @inheritdoc
207+ * { @inheritdoc}
198208 */
199- public function column ($ field , $ db = null ) {
200- if ($ field == '_id ' ) {
209+ public function column ($ field , $ db = null )
210+ {
211+ if ($ field === '_id ' ) {
201212 $ command = $ this ->createCommand ($ db );
202213 $ command ->queryParts ['fields ' ] = [];
203214 $ command ->queryParts ['_source ' ] = false ;
@@ -216,7 +227,8 @@ public function column ($field, $db = null) {
216227 return parent ::column ($ field , $ db );
217228 }
218229
219- public function getList ($ as_array = true , $ db = null , $ options = []) {
230+ public function getList ($ as_array = true , $ db = null , $ options = [])
231+ {
220232 $ rawResult = $ this ->createCommand ($ db )->getList ($ options );
221233 foreach ($ rawResult as $ k => $ v ) {
222234 $ result [] = ['gl_key ' => $ k , 'gl_value ' => $ v ];
0 commit comments