1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Suricate; |
6
|
|
|
|
7
|
|
|
class DBCollectionOneMany extends DBCollection |
8
|
|
|
{ |
9
|
|
|
/* @var string Name of parent identifier field */ |
10
|
|
|
protected $parentIdField = 'parent_id'; |
11
|
|
|
|
12
|
|
|
/* @var mixed parent identifier */ |
13
|
|
|
protected $parentId; |
14
|
|
|
|
15
|
|
|
/* @var string parent field filter name */ |
16
|
|
|
protected $parentFilterName; |
17
|
|
|
|
18
|
|
|
/* @var mixed parent filtering value */ |
19
|
|
|
protected $parentFilterType; |
20
|
|
|
|
21
|
1 |
|
public function getParentIdField(): string |
22
|
|
|
{ |
23
|
1 |
|
return $this->parentIdField; |
24
|
|
|
} |
25
|
|
|
|
26
|
1 |
|
public function getParentId() |
27
|
|
|
{ |
28
|
1 |
|
return $this->parentId; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function getParentFilterName() |
32
|
|
|
{ |
33
|
|
|
return $this->parentFilterName; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function getParentFilterType() |
37
|
|
|
{ |
38
|
|
|
return $this->parentFilterType; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Load entire table into collection |
43
|
|
|
* @return Collection Loaded collection |
44
|
|
|
*/ |
45
|
|
|
public static function loadAll() |
46
|
|
|
{ |
47
|
|
|
$calledClass = get_called_class(); |
48
|
|
|
$collection = new $calledClass(); |
49
|
|
|
if ($collection->getParentFilterType()) { |
50
|
|
|
$sql = "SELECT *"; |
51
|
|
|
$sql .= " FROM `" . $collection->getTableName() . "`"; |
52
|
|
|
$sql .= "WHERE " . $collection->parentFilterName . "=:type"; |
53
|
|
|
|
54
|
|
|
$sqlParams = ['type' => $collection->parentFilterType]; |
55
|
|
|
|
56
|
|
|
$collection->loadFromSql($sql, $sqlParams); |
57
|
|
|
|
58
|
|
|
return $collection; |
59
|
|
|
} |
60
|
|
|
return parent::loadAll(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Load items linked to a parentId |
65
|
|
|
* @param mixed $parentId Parent id description |
66
|
|
|
* @param string $parentIdField Name of parent id referencing field |
67
|
|
|
* @param \Closure|null $validate Callback use to validate add to items collection |
68
|
|
|
*/ |
69
|
|
|
public static function loadForParentId( |
70
|
|
|
$parentId, |
71
|
|
|
$parentIdField = null, |
72
|
|
|
$validate = null |
73
|
|
|
) { |
74
|
|
|
$calledClass = get_called_class(); |
75
|
|
|
$collection = new $calledClass(); |
76
|
|
|
|
77
|
|
|
if ($parentId != '') { |
78
|
|
|
$sqlParams = []; |
79
|
|
|
$dbHandler = Suricate::Database(true); |
80
|
|
|
|
81
|
|
|
if ($collection->getDBConfig() !== '') { |
82
|
|
|
$dbHandler->setConfig($collection->getDBConfig()); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$sql = "SELECT *"; |
86
|
|
|
$sql .= " FROM `" . $collection->getTableName() . "`"; |
87
|
|
|
$sql .= " WHERE"; |
88
|
|
|
if ($parentIdField !== null) { |
89
|
|
|
$sql .= "`" . $parentIdField . "`=:parent_id"; |
90
|
|
|
} else { |
91
|
|
|
$sql .= "`" . $collection->getParentIdField() . "`=:parent_id"; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if ($collection->parentFilterType !== null) { |
95
|
|
|
$sql .= |
96
|
|
|
" AND " . $collection->parentFilterName . "=:parent_type"; |
97
|
|
|
$sqlParams['parent_type'] = $collection->parentFilterType; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$sqlParams['parent_id'] = $parentId; |
101
|
|
|
$results = $dbHandler->query($sql, $sqlParams)->fetchAll(); |
102
|
|
|
|
103
|
|
|
if ($results !== false) { |
104
|
|
|
foreach ($results as $currentResult) { |
105
|
|
|
$itemName = $collection->getItemsType(); |
106
|
|
|
$item = $itemName::instanciate($currentResult); |
107
|
|
|
if ($validate === null || $validate($item)) { |
108
|
|
|
$collection->addItem($item); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$collection->parentId = $parentId; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $collection; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function setParentIdForAll($parentId) |
120
|
|
|
{ |
121
|
|
|
$this->parentId = $parentId; |
122
|
|
|
foreach (array_keys($this->items) as $key) { |
123
|
|
|
$this->items[$key]->{$this->parentIdField} = $parentId; |
124
|
|
|
} |
125
|
|
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function craftItem($itemData) |
129
|
|
|
{ |
130
|
|
|
$itemName = $this->itemsType; |
131
|
|
|
|
132
|
|
|
foreach ($itemData as $data) { |
133
|
|
|
$newItem = new $itemName(); |
134
|
|
|
$newItem->{$this->parentIdField} = $this->parentId; |
135
|
|
|
$hasData = false; |
136
|
|
|
foreach ($data as $field => $value) { |
137
|
|
|
$newItem->$field = $value; |
138
|
|
|
if ($value != '') { |
139
|
|
|
$hasData = true; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
// Only add item if there's data inside |
144
|
|
|
if ($hasData) { |
145
|
|
|
$this->addItem($newItem); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function save() |
151
|
|
|
{ |
152
|
|
|
// 1st step : delete all records for current parentId |
153
|
|
|
$sql = "DELETE FROM `" . $this->tableName . "`"; |
154
|
|
|
$sql .= " WHERE"; |
155
|
|
|
$sql .= " `" . $this->parentIdField . "`=:parent_id"; |
156
|
|
|
|
157
|
|
|
$sqlParams = ['parent_id' => $this->parentId]; |
158
|
|
|
|
159
|
|
|
$this->connectDB(); |
160
|
|
|
$this->dbLink->query($sql, $sqlParams); |
161
|
|
|
|
162
|
|
|
// 2nd step : save all current items |
163
|
|
|
foreach ($this->items as $currentItem) { |
164
|
|
|
$currentItem->save(true); // Force insert |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|