1010
1111use Akeneo \SalesForce \Connector \SalesForceClient ;
1212use Akeneo \SalesForce \Query \QueryBuilder ;
13+ use sokyrko \yii \salesforce \components \SalesforceComponent ;
1314use yii \base \InvalidCallException ;
1415use yii \base \InvalidParamException ;
1516use yii \db \ActiveQueryInterface ;
@@ -33,6 +34,9 @@ class ActiveQuery implements ActiveQueryInterface
3334 /** @var boolean */
3435 protected $ enableEmulateExecution ;
3536
37+ /** @var boolean */
38+ protected $ asArray = false ;
39+
3640 protected $ queryParts = [
3741 'select ' => [],
3842 'from ' => '' ,
@@ -41,8 +45,6 @@ class ActiveQuery implements ActiveQueryInterface
4145 'limit ' => '' ,
4246 ];
4347
44- // TODO: refactor query builder to works if andWhere called without first where
45-
4648 /**
4749 * Constructor.
4850 *
@@ -92,20 +94,24 @@ protected function parseSelect($what)
9294 */
9395 public function asArray ($ value = true )
9496 {
95- throw new InvalidCallException ( ' Not implemented yet. ' ); // TODO
97+ $ this -> asArray = $ value ;
9698 }
9799
98100 /**
99101 * Executes query and returns a single row of result.
100102 *
101103 * @param Connection $db the DB connection used to create the DB command.
102104 * If `null`, the DB connection returned by [[ActiveQueryTrait::$modelClass|modelClass]] will be used.
103- * @return ActiveRecordInterface |array|null a single row of query result. Depending on the setting of [[asArray]],
105+ * @return ActiveRecord |array|null a single row of query result. Depending on the setting of [[asArray]],
104106 * the query result may be either an array or an ActiveRecord object. `null` will be returned
105107 * if the query results in nothing.
106108 */
107109 public function one ($ db = null )
108110 {
111+ if ($ this ->enableEmulateExecution ) {
112+ return null ;
113+ }
114+
109115 $ all = $ this ->all ();
110116
111117 return reset ($ all ); // todo: refactor
@@ -185,8 +191,8 @@ public function via($relationName, callable $callable = null)
185191 * Finds the related records for the specified primary record.
186192 * This method is invoked when a relation of an ActiveRecord is being accessed in a lazy fashion.
187193 *
188- * @param string $name the relation name
189- * @param ActiveRecordInterface $model the primary model
194+ * @param string $name the relation name
195+ * @param ActiveRecordInterface|ActiveRecord $model the primary model
190196 * @return mixed the related record(s)
191197 */
192198 public function findFor ($ name , $ model )
@@ -199,16 +205,23 @@ public function findFor($name, $model)
199205 *
200206 * @param Connection $db the database connection used to execute the query.
201207 * If this parameter is not given, the `salesforce` application component will be used.
202- * @return array the query results. If the query results in nothing, an empty array will be returned.
208+ * @return array|ActiveRecord[] the query results. If the query results in nothing, an empty array will be returned.
203209 */
204210 public function all ($ db = null )
205211 {
206- /** @var SalesForceClient $client */
207- $ client = \Yii::$ app ->get ('salesforce ' )->getClient ();
212+ if ($ this ->enableEmulateExecution ) {
213+ return [];
214+ }
215+
216+ $ client = $ this ->getDb ($ db )->getClient ();
217+
218+ if ($ this ->asArray ) {
219+ return $ client ->search ($ this ->getRawQuery ());
220+ }
208221
209222 return array_map (function ($ item ) {
210223 return new $ this ->modelClass ($ item );
211- }, $ client ->search ($ this ->getRawQuery ())); // TODO: create query and then search
224+ }, $ client ->search ($ this ->getRawQuery ()));
212225 }
213226
214227 /**
@@ -234,6 +247,8 @@ public function getRawQuery()
234247 $ q ->orderBy ($ this ->parseOrderBy ($ this ->queryParts ['orderBy ' ]));
235248 }
236249
250+ // TODO: support limit and join
251+
237252 return $ q ->getQuery ();
238253 }
239254
@@ -268,12 +283,24 @@ protected function replaceSortConstWithString($sortConst)
268283 *
269284 * @param string $q the COUNT expression. Defaults to '*'.
270285 * @param Connection $db the database connection used to execute the query.
271- * If this parameter is not given, the `db ` application component will be used.
286+ * If this parameter is not given, the `salesforce ` application component will be used.
272287 * @return int number of records.
273288 */
274- public function count ($ q = '* ' , $ db = null )
289+ public function count ($ q = 'Id ' , $ db = null )
275290 {
276- throw new InvalidCallException ('Not implemented yet. ' ); // TODO
291+ $ client = $ this ->getDb ($ db )->getClient ();
292+ $ this ->select (sprintf ('COUNT(%s) ' , $ q ));
293+
294+ return $ client ->search ($ this ->getRawQuery ())[0 ]['expr0 ' ] ?? 0 ;
295+ }
296+
297+ /**
298+ * @param null $db
299+ * @return null|object|SalesforceComponent
300+ */
301+ protected function getDb ($ db = null )
302+ {
303+ return $ db ?? \Yii::$ app ->get ('salesforce ' );
277304 }
278305
279306 /**
0 commit comments