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
/
Copy pathActiveRecord.php
175 lines (154 loc) · 4.79 KB
/
ActiveRecord.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
namespace yii\vertica;
use Yii;
use yii\db\BaseActiveRecord;
use yii\helpers\ArrayHelper;
class ActiveRecord extends BaseActiveRecord
{
public static $primary = [];
/**
* @return \yii\vertica\Connection
*/
public static function getDb()
{
$db = \Yii::$app->get('vertica');
$table = static::tableName();
$db->setTable($table);
return $db;
}
/**
* @return \yii\verticaActiveQuery
*/
public static function find()
{
$called = get_called_class();
$query = Yii::createObject(ActiveQuery::className(), [$called]);
$query->from = $called::tableName();
return $query;
}
/**
* @param array $condition
* @return model
*/
public static function findOne($condition)
{
$query = static::find();
if (is_array($condition)) {
return $query->andWhere($condition)->one();
} else {
return static::get($condition);
}
}
/**
* @param array $condition
* @return array models
*/
public static function findAll($condition)
{
$query = static::find();
if (ArrayHelper::isAssociative($condition)) {
return $query->andWhere($condition)->all();
} else {
return static::mget((array) $condition);
}
}
public function getPrimaryKey($asArray = false)
{
$pk = static::primaryKey()[0];
if ($asArray) {
return [$pk => $this->$pk];
} else {
return $this->$pk;
}
}
public static function primaryKey()
{
$class = get_called_class();
if (!isset(self::$primary[$class])) {
self::$primary[$class] = self::getDb()->createCommand()->getPk();
}
return [self::$primary[$class]];
}
public function attributes()
{
return self::getDb()->createCommand()->getColumn();
}
public function arrayAttributes()
{
return self::getDb()->createCommand()->getColumn();
}
public function safeAttributes()
{
return self::getDb()->createCommand()->getColumn();
}
public static function populateRecord($record, $row)
{
$attributes = [];
if (!empty($row)) {
$attributes = $row;
}
if (!empty($row)) {
// reset fields in case it is scalar value
$arrayAttributes = $record->arrayAttributes();
foreach($row as $key => $value) {
if (!isset($arrayAttributes[$key])) {
$row[$key] = $value;
}
}
$attributes = array_merge($attributes, $row);
}
parent::populateRecord($record, $attributes);
}
public function insert($runValidation = true, $attributes = null, $options = ['op_type' => 'create'])
{
if ($runValidation && !$this->validate($attributes)) {
return false;
}
if (!$this->beforeSave(true)) {
return false;
}
$values = $this->getDirtyAttributes($attributes);
static::getDb()->createCommand()->insert(
static::tableName(),
$values
)->execute();
$changedAttributes = array_fill_keys(array_keys($values), null);
$this->setOldAttributes($values);
$this->afterSave(true, $changedAttributes);
return true;
}
public static function updateAll($attributes, $condition = [])
{
$pkName = static::primaryKey()[0];
if (empty($condition[$pkName])) {
return 0;
}
return self::getDb()->createCommand()->update(static::tableName(), $pkName, $condition[$pkName], $attributes);
}
public static function deleteAll($condition = [])
{
static::find()->where($condition)->createCommand()->delete()->execute();
return true;
}
/**
* Creates an active record instance.
*
* This method is called together with [[populateRecord()]] by [[ActiveQuery]].
* It is not meant to be used for creating new records directly.
*
* You may override this method if the instance being created
* depends on the row data to be populated into the record.
* For example, by creating a record based on the value of a column,
* you may implement the so-called single-table inheritance mapping.
* @param array $row row data to be populated into the record.
* This array consists of the following keys:
* - `_source`: refers to the attributes of the record.
* - `_type`: the type this record is stored in.
* - `_index`: the index this record is stored in.
* @return static the newly created active record
*/
public static function instantiate($row)
{
return new static;
}
}