|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
namespace Suricate\Traits; |
|
3
|
|
|
|
|
4
|
|
|
use Suricate\DBObject; |
|
5
|
|
|
|
|
6
|
|
|
trait DBObjectRelations |
|
7
|
|
|
{ |
|
8
|
|
|
protected $relations = []; |
|
9
|
|
|
protected $relationValues = []; |
|
10
|
|
|
protected $loadedRelations = []; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Get relation from its name |
|
14
|
|
|
* |
|
15
|
|
|
* @param string $name |
|
16
|
|
|
*/ |
|
17
|
1 |
|
protected function getRelation($name) |
|
18
|
|
|
{ |
|
19
|
1 |
|
if (isset($this->relationValues[$name]) && $this->isRelationLoaded($name)) { |
|
20
|
1 |
|
return $this->relationValues[$name]; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
1 |
|
if (!$this->isRelationLoaded($name)) { |
|
24
|
1 |
|
$this->loadRelation($name); |
|
25
|
1 |
|
$this->markRelationAsLoaded($name); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
1 |
|
if (isset($this->relationValues[$name])) { |
|
29
|
1 |
|
return $this->relationValues[$name]; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
return null; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Check if variable is predefined relation |
|
37
|
|
|
* @param string $name variable name |
|
38
|
|
|
* @return boolean |
|
39
|
|
|
*/ |
|
40
|
7 |
|
protected function isRelation($name) |
|
41
|
|
|
{ |
|
42
|
7 |
|
return isset($this->relations[$name]); |
|
43
|
|
|
} |
|
44
|
|
|
/** |
|
45
|
|
|
* Define object relations |
|
46
|
|
|
* |
|
47
|
|
|
* @return DBObject |
|
48
|
|
|
*/ |
|
49
|
27 |
|
protected function setRelations() |
|
50
|
|
|
{ |
|
51
|
27 |
|
$this->relations = []; |
|
52
|
|
|
|
|
53
|
27 |
|
return $this; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Mark a relation as loaded |
|
58
|
|
|
* @param string $name varialbe name |
|
59
|
|
|
* @return void |
|
60
|
|
|
*/ |
|
61
|
1 |
|
protected function markRelationAsLoaded($name) |
|
62
|
|
|
{ |
|
63
|
1 |
|
if ($this->isRelation($name)) { |
|
64
|
1 |
|
$this->loadedRelations[$name] = true; |
|
65
|
|
|
} |
|
66
|
1 |
|
} |
|
67
|
|
|
/** |
|
68
|
|
|
* Check if a relation already have been loaded |
|
69
|
|
|
* @param string $name Variable name |
|
70
|
|
|
* @return boolean |
|
71
|
|
|
*/ |
|
72
|
1 |
|
protected function isRelationLoaded($name) |
|
73
|
|
|
{ |
|
74
|
1 |
|
return isset($this->loadedRelations[$name]); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Load realation according to relation type |
|
79
|
|
|
* |
|
80
|
|
|
* @param string $name |
|
81
|
|
|
* @return void |
|
82
|
|
|
*/ |
|
83
|
1 |
|
protected function loadRelation($name) |
|
84
|
|
|
{ |
|
85
|
1 |
|
if ($this->isRelation($name)) { |
|
86
|
1 |
|
switch ($this->relations[$name]['type']) { |
|
87
|
|
|
case DBObject::RELATION_ONE_ONE: |
|
88
|
1 |
|
$this->loadRelationOneOne($name); |
|
89
|
1 |
|
return; |
|
90
|
|
|
case DBObject::RELATION_ONE_MANY: |
|
91
|
|
|
$this->loadRelationOneMany($name); |
|
92
|
|
|
return; |
|
93
|
|
|
case DBObject::RELATION_MANY_MANY: |
|
94
|
|
|
$this->loadRelationManyMany($name); |
|
95
|
|
|
return; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Load one to one relationship |
|
102
|
|
|
* |
|
103
|
|
|
* @param string $name |
|
104
|
|
|
* @return void |
|
105
|
|
|
*/ |
|
106
|
1 |
|
private function loadRelationOneOne($name) |
|
107
|
|
|
{ |
|
108
|
1 |
|
$target = $this->relations[$name]['target']; |
|
109
|
1 |
|
$source = $this->relations[$name]['source']; |
|
110
|
1 |
|
$this->relationValues[$name] = new $target(); |
|
111
|
1 |
|
$this->relationValues[$name]->load($this->$source); |
|
112
|
1 |
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Load one to many relationship |
|
116
|
|
|
* |
|
117
|
|
|
* @param string $name |
|
118
|
|
|
* @return void |
|
119
|
|
|
*/ |
|
120
|
|
|
private function loadRelationOneMany($name) |
|
121
|
|
|
{ |
|
122
|
|
|
$target = $this->relations[$name]['target']; |
|
123
|
|
|
$parentId = $this->{$this->relations[$name]['source']}; |
|
124
|
|
|
$parentIdField = isset($this->relations[$name]['target_field']) ? $this->relations[$name]['target_field'] : null; |
|
125
|
|
|
$validate = dataGet($this->relations[$name], 'validate', null); |
|
126
|
|
|
|
|
127
|
|
|
$this->relationValues[$name] = $target::loadForParentId($parentId, $parentIdField, $validate); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Load many to many relationship |
|
132
|
|
|
* |
|
133
|
|
|
* @param string $name |
|
134
|
|
|
* @return void |
|
135
|
|
|
*/ |
|
136
|
|
|
private function loadRelationManyMany($name) |
|
137
|
|
|
{ |
|
138
|
|
|
$pivot = $this->relations[$name]['pivot']; |
|
139
|
|
|
$sourceType = $this->relations[$name]['source_type']; |
|
140
|
|
|
$target = dataGet($this->relations[$name], 'target'); |
|
141
|
|
|
$validate = dataGet($this->relations[$name], 'validate', null); |
|
142
|
|
|
|
|
143
|
|
|
$this->relationValues[$name] = $pivot::loadFor($sourceType, $this->{$this->relations[$name]['source']}, $target, $validate); |
|
144
|
|
|
} |
|
145
|
|
|
} |