Skip to content

Commit 3bfc39a

Browse files
author
andreyons
committed
done: count support | asArray | emulate execution
1 parent a1e1261 commit 3bfc39a

File tree

2 files changed

+80
-16
lines changed

2 files changed

+80
-16
lines changed

src/sokyrko/yii/salesforce/data/ActiveQuery.php

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use Akeneo\SalesForce\Connector\SalesForceClient;
1212
use Akeneo\SalesForce\Query\QueryBuilder;
13+
use sokyrko\yii\salesforce\components\SalesforceComponent;
1314
use yii\base\InvalidCallException;
1415
use yii\base\InvalidParamException;
1516
use 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
/**

src/sokyrko/yii/salesforce/data/ActiveRecord.php

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
namespace sokyrko\yii\salesforce\data;
1010

1111
use yii\base\InvalidCallException;
12+
use yii\base\InvalidParamException;
1213
use yii\base\Model;
14+
use yii\base\UnknownMethodException;
1315
use yii\db\ActiveQueryInterface;
1416
use yii\db\ActiveRecordInterface;
1517

@@ -26,6 +28,9 @@ class ActiveRecord extends Model implements ActiveRecordInterface
2628
/** @var boolean */
2729
protected static $isCustom = true;
2830

31+
/** @var null|array|ActiveRecord[] */
32+
protected $relations;
33+
2934
/**
3035
* @return string
3136
*/
@@ -443,7 +448,7 @@ public function delete()
443448
*/
444449
public function getIsNewRecord()
445450
{
446-
// TODO: Implement getIsNewRecord() method.
451+
return $this->isNewRecord;
447452
}
448453

449454
/**
@@ -474,7 +479,39 @@ public function equals($record)
474479
*/
475480
public function getRelation($name, $throwException = true)
476481
{
477-
// TODO: Implement getRelation() method.
482+
$getter = 'get' . $name;
483+
try {
484+
// the relation could be defined in a behavior
485+
$relation = $this->$getter();
486+
} catch (UnknownMethodException $e) {
487+
if ($throwException) {
488+
throw new InvalidParamException(get_class($this) . ' has no relation named "' . $name . '".', 0, $e);
489+
} else {
490+
return null;
491+
}
492+
}
493+
if (!$relation instanceof ActiveQueryInterface) {
494+
if ($throwException) {
495+
throw new InvalidParamException(get_class($this) . ' has no relation named "' . $name . '".');
496+
} else {
497+
return null;
498+
}
499+
}
500+
501+
if (method_exists($this, $getter)) {
502+
// relation name is case sensitive, trying to validate it when the relation is defined within this class
503+
$method = new \ReflectionMethod($this, $getter);
504+
$realName = lcfirst(substr($method->getName(), 3));
505+
if ($realName !== $name) {
506+
if ($throwException) {
507+
throw new InvalidParamException('Relation names are case sensitive. ' . get_class($this) . " has a relation named \"$realName\" instead of \"$name\".");
508+
} else {
509+
return null;
510+
}
511+
}
512+
}
513+
514+
return $relation;
478515
}
479516

480517
/**
@@ -487,7 +524,7 @@ public function getRelation($name, $throwException = true)
487524
*/
488525
public function populateRelation($name, $records)
489526
{
490-
// TODO: Implement populateRelation() method.
527+
$this->relations[ $name ] = $records;
491528
}
492529

493530
/**

0 commit comments

Comments
 (0)