This repository has been archived by the owner on Apr 6, 2018. It is now read-only.
forked from Rustavely/yii2-vertica
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ActiveQuery.php
99 lines (88 loc) · 3.09 KB
/
ActiveQuery.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
namespace yii\vertica;
use yii\db\ActiveQueryInterface;
use yii\db\ActiveQueryTrait;
use yii\db\ActiveRelationTrait;
class ActiveQuery extends Query implements ActiveQueryInterface
{
use ActiveQueryTrait;
use ActiveRelationTrait;
/**
* Constructor.
* @param array $modelClass the model class associated with this query
* @param array $config configurations to be applied to the newly created query object
*/
public function __construct($modelClass, $config = [])
{
$this->modelClass = $modelClass;
parent::__construct($config);
}
/**
* Creates a DB command that can be used to execute this query.
* @param Connection $db the DB connection used to create the DB command.
* If null, the DB connection returned by [[modelClass]] will be used.
* @return Command the created DB command instance.
*/
public function createCommand($db = null)
{
/* @var $modelClass ActiveRecord */
$modelClass = $this->modelClass;
if ($db === null) {
$db = $modelClass::getDb();
}
return $db->createCommand(['sql' => $db->getQueryBuilder()->build($this)]);
}
/**
* Executes query and returns all results as an array.
* @param Connection $db the DB connection used to create the DB command.
* If null, the DB connection returned by [[modelClass]] will be used.
* @return array the query results. If the query results in nothing, an empty array will be returned.
*/
public function all($db = null)
{
if ($this->asArray) {
return parent::all($db);
}
$result = $this->createCommand($db)->search();
if (empty($result)) {
return [];
}
$models = $this->createModels($result);
if (!empty($this->with)) {
$this->findWith($this->with, $models);
}
foreach ($models as $model) {
$model->afterFind();
}
return $models;
}
/**
* Executes query and returns a single row of result.
* @param Connection $db the DB connection used to create the DB command.
* If null, the DB connection returned by [[modelClass]] will be used.
* @return ActiveRecord|array|null a single row of query result. Depending on the setting of [[asArray]],
* the query result may be either an array or an ActiveRecord object. Null will be returned
* if the query results in nothing.
*/
public function one($db = null)
{
if (($result = parent::one($db)) === false) {
return null;
}
if ($this->asArray) {
return $result;
} else {
/* @var $class ActiveRecord */
$class = $this->modelClass;
$model = $class::instantiate($result);
$class::populateRecord($model, $result);
if (!empty($this->with)) {
$models = [$model];
$this->findWith($this->with, $models);
$model = $models[0];
}
$model->afterFind();
return $model;
}
}
}