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