-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathClosureTableBehavior.php
530 lines (505 loc) · 19.3 KB
/
ClosureTableBehavior.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
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
<?php
/**
* ClosureTableBehavior class file.
* Provides tree set functionality for a model.
*
* @author Aidas Klimas
* @link https://github.com/AidasK/yii-closure-table-behavior/
* @version 1.1.0
*/
class ClosureTableBehavior extends CActiveRecordBehavior
{
public $closureTableName;
public $childAttribute = 'child';
public $parentAttribute = 'parent';
public $depthAttribute = 'depth';
public $isLeafParameter = 'leaf';
/**
* Finds roots
* @return CActiveRecord the owner.
*/
public function roots()
{
$owner = $this->getOwner();
$db = $owner->getDbConnection();
$criteria = $owner->getDbCriteria();
$alias = $owner->getTableAlias(true);
$closureTable = $db->quoteTableName($this->closureTableName);
$childAttribute = $db->quoteColumnName($this->childAttribute);
$parentAttribute = $db->quoteColumnName($this->parentAttribute);
$primaryKeyName = $db->quoteColumnName($owner->tableSchema->primaryKey);
$criteria->mergeWith(array(
'join' => 'LEFT JOIN ' . $closureTable . ' ct1'
. ' ON ' . $alias . '.' . $primaryKeyName . '=ct1.' . $childAttribute
. ' LEFT JOIN ' . $closureTable . ' ct2'
. ' ON ct1.' . $childAttribute . '=ct2.' . $childAttribute
. ' AND ct2.' . $parentAttribute . '<>ct1.' . $parentAttribute,
'condition' => 'ct2.' . $parentAttribute . 'IS NULL'
));
return $owner;
}
/**
* Finds descendants
* @param int|string $primaryKey.
* @param int $depth the depth.
* @return CActiveRecord the owner.
*/
public function descendantsOf($primaryKey, $depth = null)
{
/* @var $owner CActiveRecord */
$owner = $this->getOwner();
$db = $owner->getDbConnection();
$criteria = $owner->getDbCriteria();
$alias = $owner->getTableAlias(true);
$closureTable = $db->quoteTableName($this->closureTableName);
$childAttribute = $db->quoteColumnName($this->childAttribute);
$parentAttribute = $db->quoteColumnName($this->parentAttribute);
$primaryKeyName = $db->quoteColumnName($owner->tableSchema->primaryKey);
$criteria->mergeWith(array(
'join' => 'JOIN ' . $closureTable
. ' ON ' . $closureTable . '.' . $childAttribute . '='
. $alias . '.' . $primaryKeyName,
'condition' => $closureTable . '.' . $parentAttribute . '=' . $db->quoteValue($primaryKey)
));
if ($depth === null) {
$criteria->addCondition(
$closureTable . '.' . $childAttribute . '!=' . $closureTable . '.' . $parentAttribute
);
} else {
$criteria->addCondition(
$closureTable . '.' . $db->quoteColumnName($this->depthAttribute) . ' BETWEEN 1 AND ' . (int) $depth
);
}
return $owner;
}
/**
* Named scope. Gets descendants for node.
* @param int $depth the depth.
* @return CActiveRecord the owner.
*/
public function descendants($depth = null)
{
/* @var $owner CActiveRecord */
$owner = $this->getOwner();
return $this->descendantsOf($owner->getPrimaryKey(), $depth);
}
/**
* Named scope. Gets children for node (direct descendants only).
* @param int|string $primaryKey
* @return CActiveRecord the owner.
*/
public function childrenOf($primaryKey)
{
return $this->descendantsOf($primaryKey, 1);
}
/**
* Named scope. Gets children for node (direct descendants only).
* @return CActiveRecord the owner.
*/
public function children()
{
return $this->descendants(1);
}
/**
* Named scope. Gets ancestors for node.
* @param int|string $primaryKey primary key
* @param int $depth the depth.
* @return CActiveRecord the owner.
*/
public function ancestorsOf($primaryKey, $depth = null)
{
/* @var $owner CActiveRecord */
$owner = $this->getOwner();
$db = $owner->getDbConnection();
$criteria = $owner->getDbCriteria();
$closureTableAlias = $db->quoteTableName('ctp');
$this->unorderedPathOf($primaryKey);
if ($depth == null) {
$criteria->mergeWith(array(
'condition' => $closureTableAlias . '.' . $db->quoteColumnName($this->childAttribute)
. '!=' . $closureTableAlias . '.' . $db->quoteColumnName($this->parentAttribute)
));
} else {
$criteria->mergeWith(array(
'condition' => $closureTableAlias . '.' . $db->quoteColumnName($this->depthAttribute)
. ' BETWEEN 1 AND ' . (int) $depth
));
}
return $owner;
}
/**
* Named scope. Gets ancestors for node.
* @param int $depth the depth.
* @return CActiveRecord the owner.
*/
public function ancestors($depth = null)
{
/* @var $owner CActiveRecord */
$owner = $this->getOwner();
return $this->ancestorsOf($owner->getPrimaryKey(), $depth);
}
/**
* Named scope. Gets parent of node.
* @param int|string $primaryKey primary key
* @return CActiveRecord the owner.
*/
public function parentOf($primaryKey)
{
return $this->ancestorsOf($primaryKey, 1);
}
/**
* Named scope. Gets parent of node.
* @return CActiveRecord the owner.
*/
public function parent()
{
return $this->ancestors(1);
}
/**
* Named scope. Gets path to the node.
* @param int|string $primaryKey primary key
* @return CActiveRecord the owner.
*/
public function unorderedPathOf($primaryKey)
{
/* @var $owner CActiveRecord */
$owner = $this->getOwner();
$db = $owner->getDbConnection();
$criteria = $owner->getDbCriteria();
$alias = $owner->getTableAlias(true);
$closureTable = $db->quoteTableName($this->closureTableName);
$closureTableAlias = $db->quoteTableName('ctp');
$primaryKeyName = $db->quoteColumnName($owner->tableSchema->primaryKey);
$criteria->mergeWith(array(
'join' => 'JOIN ' . $closureTable . ' ' . $closureTableAlias
. ' ON ' . $closureTableAlias . '.' . $db->quoteColumnName($this->parentAttribute) . '='
. $alias . '.' . $primaryKeyName,
'condition' => $closureTableAlias . '.' . $db->quoteColumnName($this->childAttribute) . '='
. $db->quoteValue($primaryKey)
));
return $owner;
}
/**
* Named scope. Gets path to the node.
* @param int|string $primaryKey primary key
* @return CActiveRecord the owner.
*/
public function pathOf($primaryKey)
{
/* @var $owner CActiveRecord */
$owner = $this->getOwner();
$db = $owner->getDbConnection();
$this->unorderedPathOf($primaryKey);
$criteria = $owner->getDbCriteria();
$closureTableAlias = $db->quoteTableName('ctp');
$depthAttribute = $db->quoteColumnName($this->depthAttribute);
$criteria->mergeWith(array(
'order' => $closureTableAlias . '.' . $depthAttribute . ' DESC'
));
return $owner;
}
/**
* Named scope. Gets path to the node.
* @return CActiveRecord the owner.
*/
public function path()
{
/* @var $owner CActiveRecord */
$owner = $this->getOwner();
return $this->pathOf($owner->getPrimaryKey());
}
/**
* Named scope. Get path with its children.
* Warning: root node isn't returned.
*
* @return CActiveRecord the owner.
*/
public function fullPathOf($primaryKey)
{
/* @var $owner CActiveRecord */
$owner = $this->getOwner();
$db = $owner->getDbConnection();
$criteria = $owner->getDbCriteria();
$alias = $owner->getTableAlias(true);
$closureTable = $db->quoteTableName($this->closureTableName);
$parentAttribute = $db->quoteColumnName($this->parentAttribute);
$childAttribute = $db->quoteColumnName($this->childAttribute);
$primaryKeyName = $db->quoteColumnName($owner->tableSchema->primaryKey);
$criteria->mergeWith(array(
'join' => 'JOIN ' . $closureTable . ' ct1'
. ' JOIN ' . $closureTable . ' ct2'
. ' ON ct1.' . $parentAttribute . '=ct2.' . $parentAttribute
. ' AND ' . $alias . '.' . $primaryKeyName . '=ct2.' . $childAttribute
. ' AND ct2.' . $db->quoteColumnName($this->depthAttribute) . '=1',
'condition' => 'ct1.' . $childAttribute . '=' . $db->quoteValue($primaryKey)
));
return $owner;
}
/**
* Named scope. Get path with its children.
* Warning: root node isn't returned.
*
* @return CActiveRecord the owner.
*/
public function fullPath()
{
/* @var $owner CActiveRecord */
$owner = $this->getOwner();
return $this->fullPathOf($owner->getPrimaryKey());
}
/**
* Named scope. Selects leaf column which indicates if record is a leaf
* @return CActiveRecord the owner.
*/
public function leaf()
{
/* @var $owner CActiveRecord */
$owner = $this->getOwner();
$db = $owner->getDbConnection();
$criteria = $owner->getDbCriteria();
$alias = $owner->getTableAlias(true);
$closureTable = $db->quoteTableName($this->closureTableName);
$parentAttribute = $db->quoteColumnName($this->parentAttribute);
$closureTableAlias = 'ctleaf';
$primaryKeyName = $db->quoteColumnName($owner->tableSchema->primaryKey);
$select = 'ISNULL(' . $closureTableAlias . '.' . $parentAttribute . ') as ' . $this->isLeafParameter;
if ($criteria->select==='*') {
$select = $alias . '.*,' . $select;
}
$criteria->mergeWith(array(
'join' => 'LEFT JOIN ' . $closureTable . ' ' . $closureTableAlias
. ' ON ' . $closureTableAlias . '.' . $parentAttribute . '=' . $alias . '.' . $primaryKeyName
. ' AND '. $closureTableAlias . '.' . $parentAttribute . '!='
. $closureTableAlias . '.' . $db->quoteColumnName($this->childAttribute),
'select' => array(
$select
),
'group' => $alias . '.' . $primaryKeyName
));
return $owner;
}
/**
* leaf scope is required
* @return bool
*/
public function isLeaf()
{
return (boolean) $this->getOwner()->{$this->isLeafParameter};
}
/**
* Save node and insert closure table records with transaction
* @param boolean $runValidation whether to perform validation before saving the record.
* If the validation fails, the record will not be saved to database.
* @param array $attributes list of attributes that need to be saved. Defaults to null,
* meaning all attributes that are loaded from DB will be saved.
* @throws CDbException|Exception
* @return boolean whether the saving succeeds
*/
public function saveNodeAsRoot($runValidation = true, $attributes = null)
{
/* @var $owner CActiveRecord */
$owner = $this->getOwner();
$db = $owner->getDbConnection();
if ($db->getCurrentTransaction() === null) {
$transaction = $db->beginTransaction();
}
try {
if (!$owner->save($runValidation, $attributes)) {
if (isset($transaction)) {
$transaction->rollback();
}
return false;
}
$this->markAsRoot($owner->primaryKey);
if (isset($transaction)) {
$transaction->commit();
}
} catch (CDbException $e) {
if (isset($transaction)) {
$transaction->rollback();
}
throw $e;
}
return true;
}
/**
* Insert closure table records
* @param $primaryKey
* @return int
*/
public function markAsRoot($primaryKey)
{
/* @var $owner CActiveRecord */
$owner = $this->getOwner();
$db = $owner->getDbConnection();
$childAttribute = $db->quoteColumnName($this->childAttribute);
$parentAttribute = $db->quoteColumnName($this->parentAttribute);
$depthAttribute = $db->quoteColumnName($this->depthAttribute);
$closureTable = $db->quoteTableName($this->closureTableName);
$cmd = $db->createCommand(
'INSERT INTO ' . $closureTable
. '(' . $parentAttribute . ',' . $childAttribute . ',' . $depthAttribute . ') '
. 'VALUES (:nodeId,:nodeId,\'0\')'
);
return $cmd->execute(array(':nodeId' => $primaryKey));
}
/**
* Appends node to target as child (Only for new records).
* @param CActiveRecord|int|string $target where to append
* @param CActiveRecord|int|string $node node to append
* @return number of rows inserted, on fail - 0
*/
public function appendTo($target, $node = null)
{
/* @var $owner CActiveRecord */
$owner = $this->getOwner();
$db = $owner->getDbConnection();
$closureTable = $db->quoteTableName($this->closureTableName);
if ($target instanceof CActiveRecord) {
$primaryKey = $target->primaryKey;
} else {
$primaryKey = $target;
}
if ($node === null) {
$node = $owner;
}
if ($node instanceof CActiveRecord) {
$nodeId = $node->primaryKey;
} else {
$nodeId = $node;
}
$childAttribute = $db->quoteColumnName($this->childAttribute);
$parentAttribute = $db->quoteColumnName($this->parentAttribute);
$depthAttribute = $db->quoteColumnName($this->depthAttribute);
$cmd = $db->createCommand(
'INSERT INTO ' . $closureTable
. '(' . $parentAttribute . ',' . $childAttribute . ',' . $depthAttribute . ') '
. 'SELECT ' . $parentAttribute . ',:nodeId'
. ',' . $depthAttribute . '+1 '
. 'FROM ' . $closureTable
. 'WHERE ' . $childAttribute . '=:pk '
. 'UNION ALL SELECT :nodeId,:nodeId,\'0\''
);
return $cmd->execute(array(':nodeId' => $nodeId, ':pk' => $primaryKey));
}
/**
* Appends target to node as child.
* @param CActiveRecord $target the target.
* @return boolean whether the appending succeeds.
*/
public function append(CActiveRecord $target)
{
return $target->appendTo($this->getOwner());
}
/**
* Move node
* @param CActiveRecord|int|string $target
* @param CActiveRecord|int|string $node if null, owner id will be used
* @throws CDbException|Exception
*/
public function moveTo($target, $node = null)
{
/* @var $owner CActiveRecord */
$owner = $this->getOwner();
$db = $owner->getDbConnection();
$closureTable = $db->quoteTableName($this->closureTableName);
if ($target instanceof CActiveRecord) {
$targetId = $target->primaryKey;
} else {
$targetId = $target;
}
if ($node === null) {
$node = $owner;
}
if ($node instanceof CActiveRecord) {
$nodeId = $node->primaryKey;
} else {
$nodeId = $node;
}
$childAttribute = $db->quoteColumnName($this->childAttribute);
$parentAttribute = $db->quoteColumnName($this->parentAttribute);
$depthAttribute = $db->quoteColumnName($this->depthAttribute);
if ($db->getCurrentTransaction() === null) {
$transaction = $db->beginTransaction();
}
try {
$cmd = $db->createCommand(
'DELETE a FROM ' . $closureTable . ' a '
. 'JOIN ' . $closureTable . ' d ON a.' . $childAttribute . '=d.' . $childAttribute
. 'LEFT JOIN ' . $closureTable . ' x ON x.' . $parentAttribute . '=d.' . $parentAttribute
. 'AND x.' . $childAttribute . '=a.' . $parentAttribute
. 'WHERE d.' . $parentAttribute . '=? AND x.' . $parentAttribute . ' IS NULL'
);
if (!$cmd->execute(array($nodeId))) {
throw new CDbException('Node had no records in closure table', 200);
}
$cmd = $db->createCommand(
'INSERT INTO ' . $closureTable . '(' . $parentAttribute . ',' . $childAttribute . ',' . $depthAttribute . ')'
. 'SELECT u.' . $parentAttribute . ',b.' . $childAttribute
. ',u.' . $depthAttribute . '+b.' . $depthAttribute . '+1 '
. 'FROM ' . $closureTable . ' u JOIN ' . $closureTable . ' b '
. 'WHERE b.' . $parentAttribute . '=? AND u.' . $childAttribute . '=?'
);
if (!$cmd->execute(array($nodeId, $targetId))) {
throw new CDbException('Target node does not exist', 201);
}
if (isset($transaction)) {
$transaction->commit();
}
} catch (CDbException $e) {
if (isset($transaction)) {
$transaction->rollback();
}
throw $e;
}
}
/**
* Deletes node and it's descendants.
* @param $primaryKey
* @return int number of rows and relations deleted (Note, number may differ in different mysql versions)
*/
public function deleteNode($primaryKey = null)
{
/* @var $owner CActiveRecord */
$owner = $this->getOwner();
if ($primaryKey === null) {
$primaryKey = $owner->primaryKey;
}
$db = $owner->getDbConnection();
$closureTable = $db->quoteTableName($this->closureTableName);
$childAttribute = $db->quoteColumnName($this->childAttribute);
$primaryKeyName = $db->quoteColumnName($owner->tableSchema->primaryKey);
$cmd = $db->createCommand(
'DELETE t, f '
. 'FROM ' . $closureTable . ' t '
. 'JOIN ' . $closureTable . ' tt ON t.' . $childAttribute . '= tt.' . $childAttribute
. 'JOIN ' . $owner->tableName() . ' f ON t.' . $childAttribute . '=f.' . $primaryKeyName
. 'WHERE tt.' . $db->quoteColumnName($this->parentAttribute) . '=?'
);
return $cmd->execute(array($primaryKey));
}
/**
* Delete all records from closure table associated with this record
* @param $primaryKey
* @return int number of rows deleted
*/
public function detachNode($primaryKey = null)
{
/* @var $owner CActiveRecord */
$owner = $this->getOwner();
if ($primaryKey === null) {
$primaryKey = $owner->primaryKey;
}
$db = $owner->getDbConnection();
$closureTable = $db->quoteTableName($this->closureTableName);
$childAttribute = $db->quoteColumnName($this->childAttribute);
$primaryKeyName = $db->quoteColumnName($owner->tableSchema->primaryKey);
$cmd = $db->createCommand(
'DELETE t '
. 'FROM ' . $closureTable . ' t '
. 'JOIN ' . $closureTable . ' tt ON t.' . $childAttribute . '= tt.' . $childAttribute
. 'WHERE tt.' . $db->quoteColumnName($this->parentAttribute) . '=?'
);
return $cmd->execute(array($primaryKey));
}
}