-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEntity.php
376 lines (324 loc) · 10.2 KB
/
Entity.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
<?php
/**
* TODO 1) move caching to other class
* TODO 2) move attribute storage to other class
* TODO 3) make wise caching and cache cleaning
* TODO 4) make attribute validation
*/
abstract class RM_Entity
implements
RM_Entity_Search_Result_Interface,
RM_Interface_Identifiable,
RM_Interface_Savable {
const TABLE_NAME = null;
const CACHE_NAME = null;
const AUTO_CACHE = true;
protected static $_properties = array();
/**
* @var RM_Entity_Worker_Data
*/
protected $_entityDataWorker;
/**
* @var RM_Entity_Worker_Cache
*/
protected $_cacheWorker;
private $_calledClass;
public function __construct($data = null) {
$this->_calledClass = get_called_class();
$this->_entityDataWorker = new RM_Entity_Worker_Data(
$this->_calledClass,
is_null($data) ? new stdClass() : $data
);
}
public function destroy() {
static::_getStorage()->clearData($this->getId());
}
/* Manipulation data block */
public function getId() {
return $this->__get(
static::_getKeyAttributeProperties()->getName()
);
}
public function __get($name) {
$val = $this->_entityDataWorker->getValue($name);
if (is_null($val)) {
throw new Exception("Try to get unexpected attribute {$name}");
} else {
return $val;
}
}
public function __set($name, $value) {
if (is_null($this->_entityDataWorker->setValue($name, $value))) {
throw new Exception("Try to set unexpected attribute {$name}");
}
}
public function save() {
if ($this->_entityDataWorker->save()) {
if (static::AUTO_CACHE) {
$this->__refreshCache();
}
}
}
/* Cache data block list */
protected static function _clearCacheList($key) {
static::getCacher()->remove($key);
}
protected static function _loadList($key) {
return static::getCacher()->load($key);
}
protected function _cacheList(array $data, $key) {
static::getCacher()->cache($data, $key, array());
}
/* Cache data block item */
protected function _getCacheWorker() {
if (is_null($this->_cacheWorker)) {
$this->_cacheWorker = new RM_Entity_Worker_Cache($this->_calledClass);
}
return $this->_cacheWorker;
}
public function __refreshCache() {
$this->__cache();
}
protected function __cachePrepare() {
}
protected function __getCacheTags() {
return array((string)$this->getId());
}
protected function __cacheEntity($key) {
$this->_getCacheWorker()->cache($this, $key, $this->__getCacheTags());
}
protected function __cache() {
$this->__cachePrepare();
$this->__cacheEntity($this->getId());
}
protected function __cleanCache() {
$this->_getCacheWorker()->remove((string)$this->getId());
}
protected static function __load($key) {
return static::getCacher()->load($key);
}
/* Attribute process block */
/**
* @static
* @return RM_Entity_Attribute_Properties[]
*/
public static function getAttributesProperties() {
return static::_getStorage()->getProperties();
}
public static function getKeyAttributeName() {
return self::_getKeyAttributeProperties()->getName();
}
public static function getKeyAttributeField() {
return self::_getKeyAttributeProperties()->getFieldName();
}
protected static function &_getKeyAttributeProperties() {
return static::_getStorage()->getKeyProperties();
}
public static function _getDbAttributes() {
return static::_getStorage()->getFieldNames();
}
public static function _getSecondaryDbAttributes() {
$fields = static::_getDbAttributes();
foreach ($fields as $key => $field) {
if ($field === static::_getKeyAttributeProperties()->getName()) {
unset($fields[$key]);
}
}
return $fields;
}
/* Load entities block */
/**
* @return Zend_Db_Adapter_Abstract
*/
public static function getDb() {
return RM_Entity_Db::getInstance()->getConnection(get_called_class());
}
public static function _setSelectRules(Zend_Db_Select $select) { }
/**
* @static
* @param array $options
* @throws Exception
* @return Zend_Db_Select
*/
public static function _getSelect(array $options = array()) {
if (is_null(static::TABLE_NAME)) {
throw new Exception('Table name not setted');
}
$select = static::getDb()->select();
/* @var $select Zend_Db_Select */
$select->from(static::TABLE_NAME, static::_getDbAttributes());
static::_setSelectRules($select);
if (isset($options['no_rule']) && $options['no_rule']) {
// clear where (removes deleted status condition)
$select->reset(Zend_Db_Select::WHERE);
}
return $select;
}
/**
* @static
* @param $id
* @param array $options
* @return static
*/
public static function getById($id, array $options = array()) {
$id = (int)$id;
if (is_null($item = static::_getStorage()->getData($id))) {
if (is_null($item = static::__load($id))) {
$select = static::_getSelect($options);
$select->where(
static::TABLE_NAME . '.' . static::_getKeyAttributeProperties()->getFieldName() . ' = ' . $id
);
$item = static::_initItem($select);
if ($item instanceof self) {
$item->__cache();
}
}
static::_getStorage()->setData($item, $id);
}
return $item;
}
/**
* @return static
* @throws Exception
*/
public static function getFirst() {
$cacheName = 'FIRST';
if (is_null($item = static::_getStorage()->getData($cacheName))) {
$select = static::_getSelect();
$select->order(static::TABLE_NAME . '.' . static::_getKeyAttributeProperties()->getFieldName() . ' ASC');
$item = static::_initItem($select);
static::_getStorage()->setData($item, $cacheName);
}
return $item;
}
/**
* @return static
* @throws Exception
*/
public static function getLast() {
$select = static::_getSelect();
$select->order(static::TABLE_NAME . '.' . static::_getKeyAttributeProperties()->getFieldName() . ' DESC');
$item = static::_initItem($select);
return $item;
}
/**
* TODO cache
* @param array $conditions
* @param int $limit
* @param array $options
* @return static[]
* @throws Exception
*/
public static function find(array $conditions = array(), $limit = 0, array $options = array()) {
$select = static::_getSelect($options);
foreach ($conditions as $field => $value) {
$operand = is_array($value) ? ' IN (?)' : ' = ?';
$select->where($field . $operand, $value);
}
if ($limit !== 0) {
$select->limit($limit);
}
return static::_initList($select, array());
}
/**
* TODO cache
* @param array $conditions
* @param array $options
* @return static
*/
public static function findOne(array $conditions, array $options = array()) {
$select = static::_getSelect($options);
foreach ($conditions as $field => $value) {
$operand = is_array($value) ? ' IN (?)' : ' = ?';
$select->where($field . $operand, $value);
}
return static::_initItem($select);
}
/**
* @return static[]
* @throws Exception
*/
public static function getList() {
return static::_initList(
static::_getSelect(),
func_get_args()
);
}
/**
* @param Zend_Db_Select $select
* @return static
*/
public static function _initItem(Zend_Db_Select $select) {
$select->limit(1);
if (($data = static::getDb()->fetchRow($select)) !== false) {
return static::_initItemFromData($data);
} else {
return null;
}
}
public static function column($column) {
return static::TABLE_NAME . '.' . $column;
}
/**
* @param $data
* @return static
*/
protected static function _initItemFromData($data) {
return new static($data);
}
/**
* @param RM_Query_Where $where
* @param RM_Query_Join $join
* @return int
*/
public static function getCount(
RM_Query_Where $where = null,
RM_Query_Join $join = null
) {
$select = static::_getSelect();
if ($where instanceof RM_Query_Where) {
$where->improveQuery($select);
}
if ($join instanceof RM_Query_Join) {
$join->improveQuery($select);
}
$select->limit(1);
return RM_Query_Exec::getRowCount(
$select,
join('.', array(
static::TABLE_NAME,
static::getKeyAttributeField()
))
);
}
/**
* @param Zend_Db_Select $select
* @param array $queryComponents
* @return static[]
*/
public static function _initList(
Zend_Db_Select $select,
array $queryComponents
) {
$list = RM_Query_Exec::select($select, $queryComponents);
foreach ($list as &$item) {
$item = static::_initItemFromData($item);
}
return $list;
}
/* Event manager */
public static function getEntityEventManager() {
return self::_getStorage()->getEventManager();
}
/* Entity storage data block */
public static function &_getStorage() {
$storage = RM_Entity_Storage::getInstance(get_called_class());
if (!is_array($storage->getProperties())) {
$storage->parse(static::$_properties);
}
return $storage;
}
public static function getCacher() {
return static::_getStorage()->getCacher();
}
}