-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathCEntity.php
385 lines (326 loc) · 7.87 KB
/
CEntity.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
<?php
/**
*
* 实体类
* - 读写分离
* - ID生成器
* - 工作单元
* - 乐观离线锁
* @author lifei
*
*/
class CEntity extends CActiveRecord
{
/**
* 读写分离
* @var boolean
*/
private $_isDbReading = TRUE;
protected $cacheDuration = 3600;
private $_isdirty = false;
private $_dirtykeys = array();
private $_disableCaching = false;
private $_isReadingFromCache = false;
private $_old = array();
private $_isRemoved = false;
/** 乐观离线锁的key字段 */
protected $versionKey = false;
/** 乐观离线锁的预存值 */
protected $versionValue = 0;
/**
*
* @see CActiveRecord::getOldPrimaryKey()
*/
final public function getOldPrimaryKey()
{
$pk = $this->getOldPrimaryKey();
if(!$this->versionKey)
{
return $pk;
}
if(is_array($pk)) {
if(isset($pk[$this->versionKey])) {
return array($this->versionKey => $this->versionValue) + $pk;
}
return $pk;
}
return array($this->primaryKey() => $pk, $this->versionKey => $this->versionValue);
}
/**
* Updates the row represented by this active record.
* All loaded attributes will be saved to the database.
* Note, validation is not performed in this method. You may call {@link validate} to perform the validation.
* @param array $attributes list of attributes that need to be saved. Defaults to null,
* meaning all attributes that are loaded from DB will be saved.
* @return boolean whether the update is successful
* @throws CException if the record is new
*/
public function update($attributes=null)
{
if(!$this->_isdirty) {
return true;
}
$attributes = array_unique($this->_dirtykeys);
if($this->getIsNewRecord())
throw new CDbException(Yii::t('yii','The active record cannot be updated because it is new.'));
if($this->beforeSave())
{
Yii::trace(get_class($this).'.update()','system.db.ar.CActiveRecord');
if($this->_pk===null)
$this->_pk=$this->getPrimaryKey();
if(0 == $this->updateByPk($this->getOldPrimaryKey(),$this->getAttributes($attributes)))
{
return false;
}
$this->_pk=$this->getPrimaryKey();
$this->afterSave();
return true;
}
else
return false;
}
/**
* @return boolean
*/
public function isDirty()
{
return $this->_isdirty && !empty($this->_dirtykeys);
}
protected function afterConstruct()
{
parent::afterConstruct();
if($this->isNewRecord && $this->getPrimaryKey() == NULL) {
$this->setPrimaryKey(DbIDGenerator::nextId());
}
}
protected function beforeSave()
{
// 时间戳
if($this->isNewRecord) {
$this->createtime=new CDbExpression('NOW()');
$this->updatetime=new CDbExpression('NOW()');
}
else {
$this->updatetime=new CDbExpression('NOW()');
}
// Dao更新状态
$this->setDbWriting();
// 乐观锁更新
if($this->versionKey && isset($this->{$this->versionKey})) {
$this->versionValue = $this->{$this->versionKey};
$this->{$this->versionKey}++;
}
return parent::beforeSave();
}
protected function beforeDelete()
{
$this->setDbWriting();
return parent::beforeDelete();
}
protected function beforeFind()
{
$this->enableCache();
$this->setDbReading();
parent::beforeFind();
}
protected function afterSave()
{
parent::afterSave();
}
/**
* (non-PHPdoc)
* @see CActiveRecord::afterFind()
*/
protected function afterFind()
{
// 如果开启了cache
// 实体不是从缓存读取的 或者 实体有修改
if($this->isCacheEnable() && (!$this->_isReadingFromCache || $this->isDirty())) {
$this->saveToCache();
}
parent::afterFind();
}
protected function saveToCache()
{
$dbConnection = $this->getDbConnection();
$cache = $this->getCacheInstance();
$cacheKey = $this->getCackeKey();
if($cache && isset($cache,$cacheKey))
{
$cache->set($cacheKey, $this->_attributes, $this->cacheDuration, null);
}
}
/**
* @see CActiveRecord::refresh()
*/
public function refresh() {
$this->resetReadingFromCache();
$this->disableCache();
$result = parent::refresh();
$this->enableCache();
return $result;
}
/**
* @see CActiveRecord::__set()
*/
public function __set($name, $value) {
if($this->getAttribute($name) == $value)
{
return;
}
parent::__set($name, $value);
if(!$this->getIsNewRecord() && $this->hasAttribute($name))
{
$this->_dirtykeys[] = $name;
$this->_isdirty = true;
}
}
public function isDbReading()
{
return $this->_isDbReading;
}
/**
* 标记为删除
*/
public function remove()
{
$this->_isRemoved = true;
}
public function getIsRemoveRecord()
{
return $this->_isRemoved;
}
//////////////////////////////////////////////////////////////////////////////////////////////
// DAO方法 //
//////////////////////////////////////////////////////////////////////////////////////////////
protected function getCacheInstance()
{
$dbConnection = $this->getDbConnection();
if($dbConnection->queryCachingCount>0
&& $dbConnection->queryCachingDuration>0
&& $dbConnection->queryCacheID!==false
&& ($cache=Yii::app()->getComponent($dbConnection->queryCacheID))!==null)
{
return $cache;
}
$this->disableCache();
return false;
}
/**
* @see CActiveRecord::instantiate()
*/
protected function instantiate($attributes) {
$record = parent::instantiate($attributes);
$record->_old = $attributes;
if($this->_isReadingFromCache) {
$record->setReadingFromCache();
}
return $record;
}
/**
*
* @param mixed $pk 主键
* @return string
*/
public function getCackeKey($pk = null) {
if(null == $pk)
{
$pk = $this->getPrimaryKey();
}
if($pk == null)
{
return false;
}
if(is_array($pk)) {
return Yii::app()->name . get_class($this).join("-", $pk);
} else {
return Yii::app()->name . get_class($this).$pk;
}
}
/**
*
* @return mixed
*/
private function readFromCache($pk=null)
{
if($this->isCacheEnable())
{
return false;
}
$dbConnection = $this->getDbConnection();
$cache = $this->getCacheInstance();
$cacheKey = $this->getCackeKey($pk);
if($cache && ($result=$cache->get($cacheKey))!==false)
{
Yii::trace('Query result found in cache','system.db.CDbCommand');
return $result;
}
return false;
}
/**
* @see CActiveRecord::findByPk()
* @return CEntity
*/
public function findByPk($pk, $condition = '', $params = array())
{
$this->setDbReading();
if(($result = $this->readFromCache($pk)) !== false) {
$this->setReadingFromCache();
$record = $this->populateRecord($result, true);
$this->resetReadingFromCache();
return $record;
} else {
$this->resetReadingFromCache();
return parent::findByPk($pk);
}
}
/**
* @see CActiveRecord::updateAll()
* @deprecated
*/
public function updateAll($attributes, $condition = '', $params = array()) {
$this->setDbWriting();
return parent::updateAll($attributes, $condition, $params);
}
/**
* @see CActiveRecord::deleteAll()
* @deprecated
*/
public function deleteAll($condition = '', $params = array()) {
$this->setDbWriting();
return parent::deleteAll($condition, $params);
}
protected function setDbReading()
{
$this->_isDbReading = true;
}
public function setDbWriting()
{
$this->_isDbReading = false;
}
private function disableCache()
{
$this->_disableCaching = true;
}
private function enableCache()
{
$this->_disableCaching = false;
}
private function setReadingFromCache()
{
$this->_isReadingFromCache = true;
}
private function resetReadingFromCache()
{
if($this->_isReadingFromCache)
$this->_isReadingFromCache = false;
}
/**
*
* @return boolean
*/
private function isCacheEnable()
{
return !$this->_disableCaching;
}
}