1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* KumbiaPHP web & app Framework. |
4
|
|
|
* |
5
|
|
|
* LICENSE |
6
|
|
|
* |
7
|
|
|
* This source file is subject to the new BSD license that is bundled |
8
|
|
|
* with this package in the file LICENSE.txt. |
9
|
|
|
* It is also available through the world-wide-web at this URL: |
10
|
|
|
* http://wiki.kumbiaphp.com/Licencia |
11
|
|
|
* If you did not receive a copy of the license and are unable to |
12
|
|
|
* obtain it through the world-wide-web, please send an email |
13
|
|
|
* to [email protected] so we can send you a copy immediately. |
14
|
|
|
* |
15
|
|
|
* @category Kumbia |
16
|
|
|
* |
17
|
|
|
* @copyright 2005 - 2016 Kumbia Team (http://www.kumbiaphp.com) |
18
|
|
|
* @license http://wiki.kumbiaphp.com/Licencia New BSD License |
19
|
|
|
*/ |
20
|
|
|
namespace Kumbia\ActiveRecord; |
21
|
|
|
|
22
|
|
|
use \PDO; |
23
|
|
|
use \PDOStatement; |
24
|
|
|
use \PDOException; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Base del ORM ActiveRecord. |
28
|
|
|
*/ |
29
|
|
|
abstract class BaseRecord |
30
|
|
|
{ |
31
|
|
|
|
32
|
|
|
public const VERSION = '0.5.8'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Database por defecto usa default. |
36
|
|
|
* |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
protected static $database = 'default'; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Nombre de la tabla. |
43
|
|
|
* |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
protected static $table = ''; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* PK por defecto, si no existe mira en metadata. |
50
|
|
|
* |
51
|
|
|
* @var string|null |
52
|
|
|
*/ |
53
|
|
|
protected static $pk = 'id'; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Constructor. |
57
|
|
|
* |
58
|
|
|
* @param array $data |
59
|
|
|
*/ |
60
|
|
|
public function __construct(array $data = []) |
61
|
|
|
{ |
62
|
|
|
$this->dump($data); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get the Primary Key value for the object |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
|
|
public function pk(): string |
70
|
|
|
{ |
71
|
|
|
return $this->{static::$pk}; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Cargar datos al objeto. |
76
|
|
|
* |
77
|
|
|
* @param array $data |
78
|
|
|
*/ |
79
|
|
|
public function dump(array $data = []): void |
80
|
|
|
{ |
81
|
|
|
foreach ($data as $k => $v) { |
82
|
|
|
$this->$k = $v; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Listado de los campos. |
88
|
|
|
* |
89
|
|
|
* @return string[] |
90
|
|
|
*/ |
91
|
|
|
public function getFields(): array |
92
|
|
|
{ |
93
|
|
|
return \array_keys(\get_object_vars($this)); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Alias de los campos. |
98
|
|
|
* |
99
|
|
|
* @return string[] |
100
|
|
|
*/ |
101
|
|
|
public function getAlias(): array |
102
|
|
|
{ |
103
|
|
|
//$humanize = function () |
104
|
|
|
return \array_map('\ucwords', $this->getFields()); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Verifica que PK este seteado. |
109
|
|
|
* |
110
|
|
|
* @return bool |
111
|
|
|
*/ |
112
|
|
|
protected function hasPK(): bool |
113
|
|
|
{ |
114
|
|
|
return isset($this->{static::$pk}); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Get the name of the primary key |
119
|
|
|
* |
120
|
|
|
* @return string |
121
|
|
|
*/ |
122
|
|
|
public static function getPK(): string |
123
|
|
|
{ |
124
|
|
|
return static::$pk ?? static::$pk = self::metadata()->getPK(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Obtiene nombre de tabla en la bd. |
129
|
|
|
* |
130
|
|
|
* @return string smallcase del nombre de la clase |
131
|
|
|
*/ |
132
|
|
|
public static function getTable(): string |
133
|
|
|
{ |
134
|
|
|
if (static::$table) { |
135
|
|
|
return static::$table; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$split = \explode('\\', static::class); |
139
|
|
|
$table = \preg_replace('/[A-Z]/', '_$0', \lcfirst(\end($split))); |
140
|
|
|
|
141
|
|
|
return static::$table = \strtolower($table); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Obtiene el schema al que pertenece. |
146
|
|
|
* |
147
|
|
|
* @return string |
148
|
|
|
*/ |
149
|
|
|
public static function getSchema(): string |
150
|
|
|
{ |
151
|
|
|
return ''; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Obtiene la combinación de esquema y tabla. |
156
|
|
|
* |
157
|
|
|
* @return string |
158
|
|
|
*/ |
159
|
|
|
public static function getSource(): string |
160
|
|
|
{ |
161
|
|
|
$source = static::getTable(); |
162
|
|
|
if ($schema = static::getSchema()) { |
163
|
|
|
$source = "$schema.$source"; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return $source; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Obtiene la conexión que se utilizará (contenidas en databases.php). |
171
|
|
|
* |
172
|
|
|
* @return string |
173
|
|
|
*/ |
174
|
|
|
public static function getDatabase(): string |
175
|
|
|
{ |
176
|
|
|
return static::$database; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Obtiene metadatos. |
181
|
|
|
* |
182
|
|
|
* @return Metadata\Metadata |
183
|
|
|
*/ |
184
|
|
|
public static function metadata(): Metadata\Metadata |
185
|
|
|
{ |
186
|
|
|
return Metadata\Metadata::get( |
187
|
|
|
static::getDatabase(), |
188
|
|
|
static::getTable(), |
189
|
|
|
static::getSchema() |
190
|
|
|
); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Obtiene manejador de conexion a la base de datos. |
195
|
|
|
* |
196
|
|
|
* @return \PDO |
197
|
|
|
*/ |
198
|
|
|
protected static function dbh(): \PDO |
199
|
|
|
{ |
200
|
|
|
return Db::get(static::getDatabase()); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Consulta sql preparada. |
205
|
|
|
* |
206
|
|
|
* @param string $sql |
207
|
|
|
* |
208
|
|
|
* @throws \PDOException |
209
|
|
|
* @return \PDOStatement |
210
|
|
|
*/ |
211
|
|
|
public static function prepare(string $sql): PDOStatement |
212
|
|
|
{ |
213
|
|
|
$sth = self::dbh()->prepare($sql); |
214
|
|
|
$sth->setFetchMode(\PDO::FETCH_CLASS, static::class); |
215
|
|
|
|
216
|
|
|
return $sth; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Retorna el último ID insertado. |
221
|
|
|
* |
222
|
|
|
* @return string |
223
|
|
|
*/ |
224
|
|
|
public static function lastInsertId(): string |
225
|
|
|
{ |
226
|
|
|
return self::dbh()->lastInsertId(); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Consulta sql. |
231
|
|
|
* |
232
|
|
|
* @param string $sql |
233
|
|
|
* |
234
|
|
|
* @throws \PDOException |
235
|
|
|
* @return \PDOStatement |
236
|
|
|
*/ |
237
|
|
|
public static function sql(string $sql): PDOStatement |
238
|
|
|
{ |
239
|
|
|
return self::dbh()->query($sql, \PDO::FETCH_CLASS, static::class); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Ejecuta consulta sql. |
244
|
|
|
* |
245
|
|
|
* @param string $sql |
246
|
|
|
* @param array $values valores |
247
|
|
|
* |
248
|
|
|
* @throws PDOException |
249
|
|
|
* @return bool|PDOStatement |
250
|
|
|
*/ |
251
|
|
|
public static function query(string $sql, array $values = []) |
252
|
|
|
{ |
253
|
|
|
if (empty($values)) { |
254
|
|
|
return self::sql($sql); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
$sth = self::prepare($sql); |
258
|
|
|
|
259
|
|
|
return $sth->execute($values) ? $sth : \false; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Verifica si existe el registro. |
264
|
|
|
* |
265
|
|
|
* @param string $pk valor para clave primaria |
266
|
|
|
* @return bool |
267
|
|
|
*/ |
268
|
|
|
public static function exists($pk): bool |
269
|
|
|
{ |
270
|
|
|
$source = static::getSource(); |
271
|
|
|
$pkField = static::getPK(); |
272
|
|
|
|
273
|
|
|
return self::query("SELECT COUNT(*) AS count FROM $source WHERE $pkField = ?", [$pk])->fetch()->count > 0; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Paginar consulta sql. |
278
|
|
|
* |
279
|
|
|
* @param string $sql consulta select sql |
280
|
|
|
* @param int $page numero de pagina |
281
|
|
|
* @param int $perPage cantidad de items por pagina |
282
|
|
|
* @param array $values valores |
283
|
|
|
* @return Paginator |
284
|
|
|
*/ |
285
|
|
|
public static function paginateQuery(string $sql, int $page, int $perPage, array $values = []): Paginator |
286
|
|
|
{ |
287
|
|
|
return new Paginator(static::class, $sql, $page, $perPage, $values); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Devuelve el nombre del drive PDO utilizado. |
292
|
|
|
* |
293
|
|
|
* @return string |
294
|
|
|
*/ |
295
|
|
|
public static function getDriver(): string |
296
|
|
|
{ |
297
|
|
|
return self::dbh()->getAttribute(\PDO::ATTR_DRIVER_NAME); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* Comienza una trasacción. |
302
|
|
|
* |
303
|
|
|
* @throws PDOException If there is already a transaction started or the driver does not support transactions |
304
|
|
|
* @return bool |
305
|
|
|
*/ |
306
|
|
|
public static function begin(): bool |
307
|
|
|
{ |
308
|
|
|
return self::dbh()->beginTransaction(); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* Da marcha atrás a una trasacción. |
313
|
|
|
* |
314
|
|
|
* @throws PDOException if there is no active transaction. |
315
|
|
|
* @return bool |
316
|
|
|
*/ |
317
|
|
|
public static function rollback(): bool |
318
|
|
|
{ |
319
|
|
|
return self::dbh()->rollBack(); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* Realiza el commit de una trasacción. |
324
|
|
|
* |
325
|
|
|
* @throws \PDOException if there is no active transaction. |
326
|
|
|
* @return bool |
327
|
|
|
*/ |
328
|
|
|
public static function commit(): bool |
329
|
|
|
{ |
330
|
|
|
return self::dbh()->commit(); |
331
|
|
|
} |
332
|
|
|
} |
333
|
|
|
|