-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLudoDBTreeCollection.php
More file actions
93 lines (87 loc) · 2.22 KB
/
LudoDBTreeCollection.php
File metadata and controls
93 lines (87 loc) · 2.22 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
<?php
/**
* Tree collection
* Date: 08.02.13
* Time: 22:00
* @package LudoDB
* @author Alf Magne Kalleland <post@dhtmlgoodies.com>
*/
/*
* Tree collection
* @package LudoDB
* @author Alf Magne Kalleland <post@dhtmlgoodies.com>
*/
/**
* LudoDBTreeCollection
*
* Example of use of LudoDBTreeCollection
*
* <code>
* class TestNodesWithLeafs extends LudoDBTreeCollection implements LudoDBService{
* protected $config = array(
* "sql" => "select * from test_node order by parent,id",
* "childKey" => "children",
* "model" => "TestNode",
* "fk" => "parent",
* "pk" => "id",
* "static" => array(
* "type" => "node"
* ),
* "merge" => array(
* array(
* "class" => "LeafNodes",
* "fk" => "parent_node_id",
* "pk" => "id"
* )
* )
* );
*
* public function validateArguments($service, $arguments){
* return count($arguments) === 0;
* }
*
* public function validateServiceData($service, $data){
* return true;
* }
*
* public function getValidServices(){
* return array('read');
* }
*
* public function shouldCache($service){
* return $service === "read";
* }
* }
* </code>
* @package LudoDB
*/
abstract class LudoDBTreeCollection extends LudoDBCollection
{
/**
* Return values in a tree structure
* @return array
*/
public function getValues()
{
$rows = parent::getValues();
$rowReferences = array();
$ret = array();
$pk = $this->parser->getPK();
$fk = $this->parser->getFK();
$childKey = $this->parser->getChildKey();
foreach ($rows as &$row) {
$rowReferences[$row[$pk]] = & $row;
if (isset($row[$fk])) {
$parent = & $rowReferences[$row[$fk]];
if (!isset($parent[$childKey])) {
$parent[$childKey] = array();
}
$parent[$childKey][] = & $row;
} else {
$ret[] = & $row;
}
unset($row[$fk]);
}
return $ret;
}
}