-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataObjects.php
More file actions
executable file
·440 lines (371 loc) · 10.2 KB
/
DataObjects.php
File metadata and controls
executable file
·440 lines (371 loc) · 10.2 KB
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
<?php
namespace ObjectRelationMapper;
abstract class DataObjects extends Common
{
protected function beforeSave() { $this->internalCalls($this->beforeSave); }
protected function afterSave() { $this->internalCalls($this->afterSave); }
protected function beforeDelete() { $this->internalCalls($this->beforeDelete); }
protected function afterDelete() { $this->internalCalls($this->afterDelete); }
protected function beforeUpdate() { $this->internalCalls($this->beforeUpdate); }
protected function afterUpdate() { $this->internalCalls($this->afterUpdate); }
protected function beforeInsert() { $this->internalCalls($this->beforeInsert); }
protected function afterInsert() { $this->internalCalls($this->afterInsert); }
protected function beforeLoad() { $this->internalCalls($this->beforeLoad); }
protected function afterLoad() { $this->internalCalls($this->afterLoad); }
protected $config;
protected $translations = Array(
'rows' => 'columns',
'child' => 'childs',
'server' => 'DbServer',
'primaryKey' => 'DbPrimaryKey',
'tableName' => 'DbTable',
'object' => 'Object'
);
public function __construct($primaryKey = NULL)
{
parent::__construct($primaryKey);
$this->config = $this->basicConfiguration;
}
protected function translateConfig()
{
$this->configurationCheck = false;
foreach ($this->config['rows'] as $row) {
$this->addColumn($row['name'], $row['alias'], 'string', '5000');
}
if(isset($this->config['child'])){
foreach ($this->config['child'] as $child) {
$this->addChild($child['object'], $child['name'], $child[0]['localKey'], $child[0]['foreignKey'], Array('delete' => $child['delete'], 'relation' => $child['possibilities']));
}
}
$this->setConfigDbServer($this->config['server']);
$this->setConfigDbPrimaryKey($this->config['primaryKey']);
$this->setConfigDbTable($this->config['tableName']);
$this->setConfigObject($this->config['object']);
foreach(array_diff_key($this->config, $this->translations) as $key => $value){
$this->basicConfiguration[$key] = $value;
}
}
public function __get($property)
{
if($property == 'objectData'){
return $this->data;
}
return parent::__get($property);
}
/**
* Insert Dat
* @return bool
*/
protected function insert()
{
if ($this->beforeInsert() === false) {
return false;
}
$this->queryBuilder->insert($this);
$this->changedVariables = Array();
if ($this->afterInsert(true) === false) {
return false;
}
return true;
}
/**
* Update dat dle PK
* @return bool
*/
protected function update()
{
if ($this->beforeUpdate() === false) {
return false;
}
$this->queryBuilder->update($this, $this->changedPrimaryKey);
$this->changedVariables = Array();
if ($this->afterUpdate(true) === false) {
return false;
}
return true;
}
/**
* Nahraje objekt z daneho storage
* @throws Exception\ORM
* @return boolean|mixed
*/
public function loadByPrimaryKey()
{
if (!isset($this->primaryKey) || empty($this->primaryKey)) {
throw new Exception\ORM('Nelze loadnout orm dle primarniho klice, protoze primarni klic neni nastaven.');
}
if ($this->beforeLoad() === false) {
return false;
}
$this->loadClassFromArray($this->queryBuilder->loadByPrimaryKey($this));
$this->changedVariables = Array();
if ($this->afterLoad(true) === false) {
return false;
}
return true;
}
/**
* Da se prepsat na cokoliv jineho v extendovane tride
*/
protected function setORMStorages()
{
$this->configStorage = 'ConfigStorage\Basic';
$this->queryBuilder = new QueryBuilder\DB(new Connector\ESDB());
}
/**
* Vrati vsechny data objektu
* @return array
*/
public function getData()
{
return $this->data + $this->childsData;
}
/**
* Nastavi childa, use with caution!
* @param string $childName
* @param mixed $childValue
*/
public function setChild($childName, $childValue)
{
$this->{$childName} = $childValue;
}
/**
* Vrati konfiguracni direktivu bud jako text, nebo jako pole hodnot
* @param string $configDirective
* @return mixed
*/
public function config($configDirective)
{
if(isset($this->translations[$configDirective])){
return $this->{'getConfig'.$this->translations[$configDirective]}();
} else {
return $this->basicConfiguration[$configDirective];
}
}
/**
* Vrati konfiguracni direktivu childu bud jako text, nebo jako pole hodnot
* @param string $childName
* @param string $configDirective
* @return mixed
*/
public function configChild($childName, $configDirective)
{
if($configDirective == 'object'){
return $this->childs[$childName]->ormName;
} elseif ($configDirective == 0 || $configDirective == '0') {
return Array(
'localKey' => $this->childs[$childName]->localKey,
'foreignKey' => $this->childs[$childName]->foreignKey,
);
} elseif ($configDirective == 'possibilities'){
return $this->childs[$childName]->additionalParams['relation'];
} elseif ($configDirective == 'name'){
return $this->childs[$childName]->alias;
} elseif ($configDirective == 'delete'){
return $this->childs[$childName]->additionalParams['delete'];
} else {
return $this->childs[$childName]->{$configDirective};
}
}
/**
* Vrati pole aliasu
* @return array
*/
public function getConfigAliases()
{
return $this->getAllAliases();
}
/**
* Vrati pole db fieldu
* @return array
*/
public function getConfigDbFields()
{
return $this->getAllDbFields();
}
/**
* Ulozi objekt do databaze nebo do jineho specifikovaneho uloziste
*/
public function save($forceInsert = false)
{
if ($this->readOnly == true) {
return true;
}
if ($this->beforeSave() === false) {
return false;
}
if ($forceInsert == true || empty($this->primaryKey)) {
$this->insert();
} else {
$this->update();
}
$this->changedVariables = Array();
if ($this->afterSave(true) === false) {
return false;
}
return true;
}
/**
* Nahraje objekt z databaze nebo z tridy Collection nebo z pameti
* vraci isLoaded
* @param bool $forceReload
* @param array $loadArray
* @param array $additionalParams
* @return bool
*/
public function load($forceReload = false, $loadArray = NULL, $additionalParams = NULL)
{
if ($this->beforeLoad() === false) {
return false;
}
// OBJECT RELATION MAPPER COMPAT HACK -- DO NOT REMOVE
if (is_array($forceReload)) {
$loadArray = $forceReload;
}
if (!is_null($loadArray)) {
$this->loadClassFromArray($loadArray);
} else {
$this->loadClassFromArray($this->queryBuilder->load($this));
}
$this->changedVariables = Array();
if ($this->afterLoad(true) === false) {
return false;
}
return true;
}
/**
* Vytvoreni childa z predanych vysledku DB
* @param string $childName
* @param array $loadArray
* @return mixed
*/
public function loadChild($childName, $loadArray)
{
$orm = $this->childs[$childName]->ormName;
/** @var $orm \ObjectRelationMapper\DataObjects */
$orm = new $orm();
$orm->load(false, $loadArray);
$this->{$childName} = $orm;
return $this->{$childName};
}
/**
* Nastavi pro object delete marku, nebo rovnou objekt smaze
* @param bool $forceDelete
* @return bool
*/
public function delete($forceDelete = false)
{
if ($this->beforeDelete() === false) {
return false;
}
if ($forceDelete == true) {
$this->queryBuilder->delete($this);
} else {
$this->deleteMark = true;
}
$this->changedVariables = Array();
if ($this->afterDelete(true) === false) {
return false;
}
return true;
}
/**
* Vrati objekty Children nodes, podle toho jak byly specifikovany v konfiguraci<br />
* (napriklad spravne propojeni tabulek atp)
* @param bool $child
* @param bool $order
* @param bool $direction
* @param bool $forceReload
* @param bool|int $limit
* @param bool|int $offset
* @return mixed
*/
public function children($child = false, $order = false, $direction = false, $forceReload = false, $limit = false, $offset = false)
{
if(!$forceReload && isset($this->childsData[$child])){
return $this->{$child};
}
$orm = $this->childs[$child]->ormName;
$sRelation = $this->childs[$child]->additionalParams['relation'];
/** @var DataObjects $orm */
$orm = new $orm();
if ($order !== false) {
$orm->setOrderingOrder($order, (($direction === false) ? Base\AORM::ORDERING_ASCENDING : $direction));
}
if ($limit !== false) {
$orm->setOrderingLimit($limit);
}
if ($offset !== false) {
$orm->setOrderingOffset($offset);
}
$localKey = $this->getAlias($this->childs[$child]->localKey);
$foreignKey = $orm->getAlias($this->childs[$child]->foreignKey);
if (!empty($this->{$localKey})) {
$orm->{$foreignKey} = $this->{$localKey};
if($sRelation == 'many'){
$this->$child = $orm->loadMultiple();
} else {
$orm->load();
$this->$child = $orm;
}
} else {
if($sRelation == 'many'){
$this->$child = Array();
} else {
$this->$child = null;
}
}
return $this->$child;
}
/**
* Slouzi pro ziskani dat pro metodu Collection::fromORM()
* Nepouzivat primo
*/
public function _fetchAll($order = null, $direction = null, $limit = null, $offset = null, $forceReload = false)
{
if($order != NULL){
if (is_array($order)) {
foreach ($order as $key => $value) {
$this->setOrderingOrder($value, ($direction[$key] == self::ORDERING_ASCENDING) ? self::ORDERING_ASCENDING : self::ORDERING_DESCENDING);
}
} else {
$this->setOrderingOrder($order, ($direction == self::ORDERING_ASCENDING) ? self::ORDERING_ASCENDING : self::ORDERING_DESCENDING);
}
}
if($limit != NULL){
$this->setOrderingLimit($limit);
}
if($offset != NULL ){
$this->setOrderingOffset($offset);
}
return $this->queryBuilder->loadMultiple($this);
}
/**
* Vrati tabulku kterou objekt vyuziva
* @return string
*/
public function getTable()
{
return $this->getConfigDbTable();
}
/**
* Vrati server, ktery trida vyuziva pro svoji funkcnost
* @return string
*/
public function getServer()
{
return $this->getConfigDbServer();
}
/**
* Vraci state tridy a promennou isLoaded
* @return boolean
*/
public function isLoaded()
{
return $this->isLoaded;
}
public function getAllDbFields($imploder = ',', $includeTableName = false)
{
return $this->getAllDbFieldsInternal($imploder, $includeTableName);
}
}