This repository has been archived by the owner on Oct 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRelation.php
334 lines (284 loc) · 9.25 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
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
<?php
namespace ext\activedocument;
use \CComponent;
/**
* BaseRelation class
*
* @version $Version: 1.0.dev.29 $
* @author $Author: intel352 $.0
*/
abstract class BaseRelation extends CComponent {
/**
* @var string name of the related object
*/
public $name;
/**
* @var string name of the related document class
*/
public $className;
/**
* @var array the inputs that will be used for the query (typical for map/reduce queries)
* array(
* array('container' => $container, 'key' => $key, 'data' => $data)
* )
*/
public $inputs = array();
/**
* @var array custom phases that should be executed (typically used for chaining multiple map & reduce phases)
* array(
* array('phase' => $phase, 'function' => $function, 'args' => $args)
* )
*/
public $phases = array();
/**
* @var array the parameters that are to be bound to the condition.
* The keys are parameter placeholder names, and the values are parameter values.
*/
public $params = array();
/**
* Search conditions
* array(
* array('column' => $column, 'keyword' => $keyword, 'like' => $like, 'escape' => $escape)
* )
*
* @var array
*/
public $search = array();
/**
* Column conditions
* array(
* array('column' => $name, 'value' => $value, 'operator' => $operator)
* )
*
* @var array
*/
public $columns = array();
/**
* Array conditions (column [not] in array)
* array(
* array('column' => $column, 'values' => $values, 'like' => $like)
* )
*
* @var array
*/
public $array = array();
/**
* Between conditions
* array(
* array('column' => $column, 'valueStart' => $valueStart, 'valueEnd' => $valueEnd)
* )
*
* @var array
*/
public $between = array();
/**
* @var string Order by clause. For {@link \ext\activedocument\Relation} descendant classes
*/
public $order = '';
protected $_scopes = array();
/**
* Constructor.
*
* @param string $name name of the relation
* @param string $className name of the related active record class
* @param array $options additional options (name=>value). The keys must be the property names of this class.
*/
public function __construct($name, $className, $options = array()) {
$this->name = $name;
$this->className = $className;
foreach ($options as $name => $value)
$this->$name = $value;
}
/**
* Array or string accepted (string format: scope1:scope2:scope3)
*
* @param mixed $scopes
*/
public function setScopes($scopes) {
if (!is_array($scopes))
$scopes = explode(':', $scopes);
$this->_scopes = array_merge($this->_scopes, $scopes);
}
public function getScopes() {
return $this->_scopes;
}
/**
* Merges this relation with a criteria specified dynamically.
*
* @param array|Criteria $criteria the dynamically specified criteria
* @return self
*/
public function mergeWith($criteria) {
if ($criteria instanceof Criteria)
$criteria = $criteria->toArray();
foreach (array('inputs', 'phases', 'params', 'search', 'columns', 'array', 'between') as $arr) {
if (isset($criteria[$arr]) && (array)$this->$arr !== (array)$criteria[$arr])
$this->$arr = array_merge((array)$this->$arr, (array)$criteria[$arr]);
}
if (isset($criteria['order']) && $this->order !== $criteria['order']) {
if ($this->order === '')
$this->order = $criteria['order'];
else if ($criteria['order'] !== '')
$this->order = $criteria['order'] . ', ' . $this->order;
}
if (isset($criteria['scopes']))
$this->setScopes($criteria['scopes']);
return $this;
}
}
/**
* StatRelation represents a statistical relational query.
*
* @version $Version: 1.0.dev.29 $
* @author $Author: intel352 $
*/
class StatRelation extends BaseRelation {
/**
* @var mixed the default value to be assigned to those records that do not
* receive a statistical query result. Defaults to 0.
*/
public $defaultValue = 0;
/**
* Merges this relation with a criteria specified dynamically.
*
* @param array $criteria the dynamically specified criteria
* @return self
*/
public function mergeWith($criteria) {
if ($criteria instanceof Criteria)
$criteria = $criteria->toArray();
parent::mergeWith($criteria);
if (isset($criteria['defaultValue']))
$this->defaultValue = $criteria['defaultValue'];
return $this;
}
}
/**
* Relation is the base class for representing active relations that bring back related objects.
*
* @version $Version: 1.0.dev.29 $
* @author $Author: intel352 $
*/
abstract class Relation extends BaseRelation {
/**
* @var bool If true, related document[s] will be stored within current document
*/
public $nested = false;
/**
* @var string|array specifies which related objects should be eagerly loaded when this related object is lazily loaded.
* For more details about this property, see {@link Document::with()}.
*/
public $with = array();
/**
* @var boolean whether this table should be joined with the primary table.
* When setting this property to be false, the table associated with this relation will
* appear in a separate JOIN statement.
* If this property is set true, then the corresponding table will ALWAYS be joined together
* with the primary table, no matter the primary table is limited or not.
* If this property is not set, the corresponding table will be joined with the primary table
* only when the primary table is not limited.
*/
public $together;
/**
* @var mixed scopes to apply
* Can be set to the one of the following:
* <ul>
* <li>Single scope: 'scopes'=>'scopeName'.</li>
* <li>Multiple scopes: 'scopes'=>array('scopeName1','scopeName2').</li>
* </ul>
*/
public $scopes;
/**
* Merges this relation with a criteria specified dynamically.
*
* @param array $criteria the dynamically specified criteria
* @return self
*/
public function mergeWith($criteria) {
if ($criteria instanceof Criteria)
$criteria = $criteria->toArray();
parent::mergeWith($criteria);
if (isset($criteria['with']))
$this->with = $criteria['with'];
if (isset($criteria['together']))
$this->together = $criteria['together'];
return $this;
}
}
/**
* BelongsToRelation represents the parameters specifying a BELONGS_TO relation.
*
* @version $Version: 1.0.dev.29 $
* @author $Author: intel352 $
*/
class BelongsToRelation extends Relation {
}
/**
* HasOneRelation represents the parameters specifying a HAS_ONE relation.
*
* @version $Version: 1.0.dev.29 $
* @author $Author: intel352 $
*/
class HasOneRelation extends Relation {
/**
* @var string the name of the relation that should be used as the bridge to this relation.
* Defaults to null, meaning don't use any bridge.
*/
public $through;
}
/**
* HasManyRelation represents the parameters specifying a HAS_MANY relation.
*
* @version $Version: 1.0.dev.29 $
* @author $Author: intel352 $
*/
class HasManyRelation extends Relation {
/**
* @var integer limit of the rows to be selected. It is effective only for lazy loading this related object. Defaults to -1, meaning no limit.
*/
public $limit = -1;
/**
* @var integer offset of the rows to be selected. It is effective only for lazy loading this related object. Defaults to -1, meaning no offset.
*/
public $offset = -1;
/**
* @var string the name of the column that should be used as the key for storing related objects.
* Defaults to null, meaning using zero-based integer IDs.
*/
public $index;
/**
* @var string the name of the relation that should be used as the bridge to this relation.
* Defaults to null, meaning don't use any bridge.
*/
public $through;
/**
* @var array Array of columns that this relation should be indexed by
*/
public $autoIndices = array();
/**
* Merges this relation with a criteria specified dynamically.
*
* @param array $criteria the dynamically specified criteria
* @return self
*/
public function mergeWith($criteria) {
if ($criteria instanceof Criteria)
$criteria = $criteria->toArray();
parent::mergeWith($criteria);
if (isset($criteria['limit']) && $criteria['limit'] > 0)
$this->limit = $criteria['limit'];
if (isset($criteria['offset']) && $criteria['offset'] >= 0)
$this->offset = $criteria['offset'];
if (isset($criteria['index']))
$this->index = $criteria['index'];
return $this;
}
}
/**
* ManyManyRelation represents the parameters specifying a MANY_MANY relation.
*
* @version $Version: 1.0.dev.29 $
* @author $Author: intel352 $
*/
class ManyManyRelation extends HasManyRelation {
}