-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathActiveRecordTrait.php
98 lines (84 loc) · 2.07 KB
/
ActiveRecordTrait.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
<?php
namespace sitkoru\cache\ar;
use yii\db\ActiveRecord;
/**
* Class ActiveRecordTrait
*
* @package sitkoru\cache\ar
*/
trait ActiveRecordTrait
{
public $fromCache = false;
public $insert = false;
public static function find()
{
return new CacheActiveQuery(get_called_class());
}
public function afterSave($insert, $changedAttributes)
{
/**
* @var $this ActiveRecord
*/
parent::afterSave($insert, $changedAttributes);
$this->insert = $insert;
ActiveQueryCacheHelper::dropCaches($this, $changedAttributes);
}
public function afterDelete()
{
/**
* @var $this ActiveRecord
*/
parent::afterDelete();
ActiveQueryCacheHelper::dropCaches($this);
}
public function refresh()
{
/**
* @var $this ActiveRecord
*/
ActiveQueryCacheHelper::dropCaches($this);
return parent::refresh();
}
/**
* @param string $class
* @param array $link
*
* @return CacheActiveQuery
*/
public function hasMany($class, $link)
{
return $this->applyDropConditions(parent::hasMany($class, $link));
}
/**
* @param string $class
* @param array $link
*
* @return CacheActiveQuery
*/
public function hasOne($class, $link)
{
return $this->applyDropConditions(parent::hasOne($class, $link));
}
/**
* @param CacheActiveQuery $query
*
* @return CacheActiveQuery
*/
private function applyDropConditions(CacheActiveQuery $query)
{
foreach ($query->link as $param => $value) {
if (isset($this->$value)) {
$query->dropCacheOnCreate($param, $this->$value);
}
}
return $query;
}
/**
* @inheritdoc
*/
public static function deleteAll($condition = '', $params = [])
{
ActiveQueryCacheHelper::dropCachesForCondition(static::className(), $condition, $params);
return parent::deleteAll($condition, $params);
}
}