This repository has been archived by the owner on Jul 25, 2022. It is now read-only.
/
Relation.php
228 lines (199 loc) · 7.8 KB
/
Relation.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
<?php
/**
* Undocumented
*/
class SQL_Relation extends AbstractModel
{
public $f1 = null; // Foreign Table (actual name)
// short_name = Foreign alias
public $t = null; // Join kind (left|right|inner|cross etc.)
public $expr = null; // Using expression when joining
public $f2 = null; // Foreign field
public $m2 = null; // Master field
public $m1 = null; // Master table (defaults to owner->table / owner->table_alias)
// $m1 == $relation->f1
public $relation = null;
public $delete_behaviour = 'cascade'; // cascade, setnull, ignore
public function init()
{
parent::init();
$this->table_alias = $this->short_name;
}
/** Second argument to addField() will specify how the field is really called */
public function addField($n, $actual_field = null)
{
$f = $this->owner->addField($n, $actual_field)->from($this);
return $f;
}
public function join($foreign_table, $master_field = null, $join_kind = null, $_foreign_alias = null)
{
return $this->owner->join($foreign_table, $master_field, $join_kind, $_foreign_alias, $this);
}
public function leftJoin($foreign_table, $master_field = null, $join_kind = null, $_foreign_alias = null)
{
return $this->owner->leftJoin($foreign_table, $master_field, $join_kind, $_foreign_alias, $this);
}
public function hasOne($model, $our_field = null, $display_field = null)
{
return $this->owner->hasOne($model, $our_field, $display_field)->from($this);
}
public function hasMany($model, $their_field = null, $our_field = null, $as_field = null)
{
return $this->owner->hasMany($model, $their_field, $our_field, $as_field)->from($this);
}
// TODO: hasMany()
public function set($foreign_table, $master_field = null, $join_kind = null, $relation = null)
{
// http://dev.mysql.com/doc/refman/5.0/en/join.html
$join_types = array(
'left', 'right', 'inner', 'cross', 'natural',
'left outer', 'right outer', 'natural left',
'natural right', 'natural left outer', 'natural right outer'
);
if ($join_kind && !in_array(strtolower($join_kind), $join_types)) {
throw $this->exception('Specify reasonable SQL join type.')
->addMoreInfo('Specified', $join_kind)
->addMoreInfo('Allowed', implode(', ', $join_types));
}
$this->relation = $relation;
// Split and deduce fields
list($f1, $f2) = explode('.', $foreign_table, 2);
if (is_object($master_field)) {
$this->expr = $master_field;
} else {
$m1 = $this->relation ?: $this->owner;
$m1 = $m1->table_alias ?: $m1->table;
// Split and deduce primary table
$m2 = $master_field;
// Identify fields we use for joins
if (is_null($f2) && is_null($m2)) {
$m2 = $f1.'_id';
}
if (is_null($m2)) {
$m2 = $this->owner->id_field;
}
$this->f1 = $f1;
$this->m1 = $m1;
$this->m2 = $m2;
}
if (is_null($f2)) {
$f2 = 'id';
}
$this->f2 = $f2;
$this->t = $join_kind ?: 'inner';
$this->fa = $this->short_name;
// Use the real ID field as defined by the model as default
$this->owner->_dsql()->join($foreign_table, $this->expr ?: ($m1.'.'.$m2), $this->t, $this->short_name);
// If our ID field is NOT used, must insert record in OTHER table first and use their primary value in OUR field
if ($this->m2 && $this->m2 != $this->owner->id_field) {
// user.contactinfo_id = contactinfo.id
$this->owner->addHook('beforeInsert', $this, array(), -5);
$this->owner->addHook('beforeModify', $this, array(), -5);
$this->owner->addHook('afterDelete', $this, array(), -5);
} elseif ($this->m2) {
// author.id = book.author_id
$this->owner->addHook('afterInsert', $this);
$this->owner->addHook('beforeModify', $this);
$this->owner->addHook('beforeDelete', $this);
}// else $m2 is not set, expression is used, so don't try to do anything unnecessary
$this->owner->addHook('beforeSave', $this);
$this->owner->addHook('beforeLoad', $this);
$this->owner->addHook('afterLoad', $this);
return $this;
}
public function beforeSave($m)
{
$this->dsql = $this->owner->_dsql()->dsql()->table($this->f1);
if ($this->owner->_dsql()->debug) {
$this->dsql->debug();
}
}
public function beforeInsert($m, $q)
{
// Insert related table data and add ID into the main query
// TODO: handle cases when $this->m1 != $this->owner->table?:$this->owner->table_alias
if ($this->delete_behaviour == 'ignore') {
return;
}
if ($this->owner->hasElement($this->m2) && $this->owner->get($this->m2) !== null) {
return; // using existing element
}
$this->dsql->set($this->f2, null);
$this->id = $this->dsql->insert();
if ($this->relation) {
$q = $this->relation->dsql;
}
$q->set($this->m2, $this->id);
}
public function afterInsert($m, $id)
{
if ($this->delete_behaviour == 'ignore') {
return;
}
$this->id = $this->dsql->set($this->f2, $this->relation ? $this->relation->id ?: $id : $id)->insert();
}
public function beforeModify($m, $q)
{
if ($this->dsql->args['set']) {
$this->dsql->where($this->f2, $this->id)->update();
}
}
public function beforeDelete($m, $id)
{
// Let's hope that cascading sorts this
$this->dsql = $this->owner->_dsql()->dsql()->table($this->f1);
if ($this->owner->_dsql()->debug) {
$this->dsql->debug();
}
if ($this->delete_behaviour == 'cascade') {
$this->dsql->del('field')->where($this->f2, $this->id)->delete();
} elseif ($this->delete_behaviour == 'ignore') {
// $this->dsql->del('field')->set($this->f2,null)->where($this->f2,$this->id)->debug()->update();
}
}
public function afterDelete($m)
{
$this->dsql = $this->owner->_dsql()->dsql()->table($this->f1);
if ($this->owner->_dsql()->debug) {
$this->dsql->debug();
}
if ($this->delete_behaviour == 'cascade') {
try {
$this->dsql->del('field')->where($this->f2, $this->id)->delete();
} catch (Exception $e) {
$this->app->caughtException($e);
}
} elseif ($this->delete_behaviour == 'ignore') {
//$this->dsql->del('field')->set($this->f2,null)->where($this->f2,$this->id)->update();
}
}
/** Add query for the relation's ID, but then remove it from results. Remove ID when unloading. */
public function beforeLoad($m, $q = null)
{
if (is_null($q)) {
return;
} // manual load
if ($this->m2 && $this->m2 != $this->owner->id_field) {
$q->field($this->m2, $this->m1, $this->short_name);
} elseif ($this->m2) {
$q->field($this->f2, $this->fa ?: $this->f1, $this->short_name);
}
}
public function afterLoad($m)
{
$this->id = $m->data[$this->short_name];
unset($m->data[$this->short_name]);
}
public function afterUnload($m)
{
$this->id = null;
}
public function fieldExpr($f)
{
return $this->owner->_dsql()->expr(
$this->owner->_dsql()->bt($this->short_name).
'.'.
$this->owner->_dsql()->bt($f)
);
}
}