|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* ActiveRecord model |
|
4
|
|
|
* |
|
5
|
|
|
* @file ActiveRecord.php |
|
6
|
|
|
* |
|
7
|
|
|
* PHP version 8.0+ |
|
8
|
|
|
* |
|
9
|
|
|
* @author Alexander Yancharuk <alex at itvault dot info> |
|
10
|
|
|
* @copyright © 2012-2021 Alexander Yancharuk |
|
11
|
|
|
* @date Втр Апр 24 21:53:04 2012 |
|
12
|
|
|
* @license The BSD 3-Clause License |
|
13
|
|
|
* <https://tldrlegal.com/license/bsd-3-clause-license-(revised)> |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace Veles\Model; |
|
17
|
|
|
|
|
18
|
|
|
use Traits\DynamicPropHandlerInterface; |
|
19
|
|
|
use Veles\DataBase\Db; |
|
20
|
|
|
use Veles\DataBase\DbFilter; |
|
21
|
|
|
use Veles\DataBase\DbPaginator; |
|
22
|
|
|
use Veles\Traits\DynamicPropHandler; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Model class using ActiveRecord pattern |
|
26
|
|
|
* |
|
27
|
|
|
* @author Alexander Yancharuk <alex at itvault dot info> |
|
28
|
|
|
*/ |
|
29
|
|
|
class ActiveRecord implements DynamicPropHandlerInterface |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* @const string|null Table name |
|
33
|
|
|
*/ |
|
34
|
|
|
const TBL_NAME = null; |
|
35
|
|
|
/** @var QueryBuilder */ |
|
36
|
|
|
protected $builder; |
|
37
|
|
|
/** @var int|null */ |
|
38
|
|
|
public $id; |
|
39
|
|
|
|
|
40
|
|
|
use DynamicPropHandler; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Update data in database |
|
44
|
|
|
* |
|
45
|
|
|
* @return bool |
|
46
|
|
|
* @throws \Exception |
|
47
|
|
|
*/ |
|
48
|
1 |
|
protected function update() |
|
49
|
|
|
{ |
|
50
|
1 |
|
$sql = $this->builder->update($this); |
|
51
|
|
|
|
|
52
|
1 |
|
return Db::query($sql); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Insert data in database |
|
57
|
|
|
* |
|
58
|
|
|
* @return bool |
|
59
|
|
|
* @throws \Exception |
|
60
|
|
|
*/ |
|
61
|
1 |
|
protected function insert() |
|
62
|
|
|
{ |
|
63
|
1 |
|
$sql = $this->builder->insert($this); |
|
64
|
1 |
|
$result = Db::query($sql); |
|
65
|
1 |
|
$this->id = Db::getLastInsertId(); |
|
66
|
|
|
|
|
67
|
1 |
|
return $result; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Method with true|false return and setting properties for current object |
|
72
|
|
|
* |
|
73
|
|
|
* @param $sql |
|
74
|
|
|
* |
|
75
|
|
|
* @return bool |
|
76
|
|
|
* @throws \Exception |
|
77
|
|
|
*/ |
|
78
|
15 |
|
protected function getResult($sql) |
|
79
|
|
|
{ |
|
80
|
15 |
|
$result = Db::row($sql); |
|
81
|
|
|
|
|
82
|
15 |
|
if (empty($result)) { |
|
83
|
7 |
|
return false; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
8 |
|
$this->setProperties($result); |
|
87
|
|
|
|
|
88
|
8 |
|
return true; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
35 |
|
public function __construct() |
|
92
|
|
|
{ |
|
93
|
35 |
|
$this->builder = new QueryBuilder; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Get data type map |
|
98
|
|
|
* |
|
99
|
|
|
* @return array |
|
100
|
|
|
*/ |
|
101
|
15 |
|
public function getMap() |
|
102
|
|
|
{ |
|
103
|
15 |
|
return $this->map; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Get data by ID |
|
108
|
|
|
* |
|
109
|
|
|
* @param int $identifier Model ID |
|
110
|
|
|
* |
|
111
|
|
|
* @return bool |
|
112
|
|
|
* @throws \Exception |
|
113
|
|
|
*/ |
|
114
|
5 |
|
public function getById($identifier) |
|
115
|
|
|
{ |
|
116
|
5 |
|
$sql = $this->builder->getById($this, $identifier); |
|
117
|
|
|
|
|
118
|
5 |
|
return $this->getResult($sql); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Get object list by filter |
|
123
|
|
|
* |
|
124
|
|
|
* @param bool|DbFilter $filter Filter object |
|
125
|
|
|
* @param bool|DbPaginator $pager Pagination object |
|
126
|
|
|
* |
|
127
|
|
|
* @return bool|array |
|
128
|
|
|
* @throws \Exception |
|
129
|
|
|
*/ |
|
130
|
3 |
|
public function getAll($filter = false, $pager = false) |
|
131
|
|
|
{ |
|
132
|
3 |
|
$sql = $this->builder->find($this, $filter); |
|
133
|
|
|
|
|
134
|
3 |
|
if ($pager instanceof DbPaginator) { |
|
135
|
3 |
|
$sql = $this->builder->setPage($sql, $pager); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
3 |
|
$result = Db::rows($sql); |
|
139
|
|
|
|
|
140
|
3 |
|
if (empty($result)) { |
|
141
|
1 |
|
return false; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
2 |
|
if ($pager instanceof DbPaginator) { |
|
145
|
2 |
|
$pager->calcMaxPages(); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
2 |
|
return $result; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Save data |
|
153
|
|
|
* |
|
154
|
|
|
* @return bool |
|
155
|
|
|
* @throws \Exception |
|
156
|
|
|
*/ |
|
157
|
1 |
|
public function save() |
|
158
|
|
|
{ |
|
159
|
1 |
|
return isset($this->id) ? $this->update() : $this->insert(); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Delete data |
|
164
|
|
|
* |
|
165
|
|
|
* @param array|bool $ids Array of ID for delete |
|
166
|
|
|
* |
|
167
|
|
|
* @return bool |
|
168
|
|
|
* @throws \Exception |
|
169
|
|
|
*/ |
|
170
|
2 |
|
public function delete($ids = false) |
|
171
|
|
|
{ |
|
172
|
2 |
|
if (!$ids) { |
|
173
|
2 |
|
if (!isset($this->id)) { |
|
174
|
1 |
|
throw new \Exception('Not found model id!'); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
1 |
|
$ids = $this->id; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
1 |
|
if (!is_array($ids)) { |
|
181
|
1 |
|
$ids = [$ids]; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
1 |
|
$sql = $this->builder->delete($this, $ids); |
|
185
|
|
|
|
|
186
|
1 |
|
return Db::query($sql); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Get unique object |
|
191
|
|
|
* |
|
192
|
|
|
* @param bool|DbFilter $filter Filter object |
|
193
|
|
|
* |
|
194
|
|
|
* @return bool |
|
195
|
|
|
* @throws \Exception |
|
196
|
|
|
*/ |
|
197
|
10 |
|
public function find($filter = false) |
|
198
|
|
|
{ |
|
199
|
10 |
|
$sql = $this->builder->find($this, $filter); |
|
200
|
|
|
|
|
201
|
10 |
|
return $this->getResult($sql); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* @param QueryBuilder $builder |
|
206
|
|
|
*/ |
|
207
|
4 |
|
public function setBuilder(QueryBuilder $builder) |
|
208
|
|
|
{ |
|
209
|
4 |
|
$this->builder = $builder; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Query with pagination |
|
214
|
|
|
* |
|
215
|
|
|
* @param string $sql Query |
|
216
|
|
|
* @param bool|DbPaginator $pager Pagination object |
|
217
|
|
|
* |
|
218
|
|
|
* @return array|bool |
|
219
|
|
|
* @throws \Exception |
|
220
|
|
|
*/ |
|
221
|
4 |
|
public function query($sql, $pager = false) |
|
222
|
|
|
{ |
|
223
|
4 |
|
if ($pager instanceof DbPaginator) { |
|
224
|
2 |
|
$sql = $this->builder->setPage($sql, $pager); |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
4 |
|
$result = Db::rows($sql); |
|
228
|
|
|
|
|
229
|
4 |
|
if (empty($result)) { |
|
230
|
1 |
|
return false; |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
3 |
|
if ($pager instanceof DbPaginator) { |
|
234
|
2 |
|
$pager->calcMaxPages(); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
3 |
|
return $result; |
|
238
|
|
|
} |
|
239
|
|
|
} |
|
240
|
|
|
|