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
|
|
|
/** |
23
|
|
|
* Base del ORM ActiveRecord. |
24
|
|
|
*/ |
25
|
|
|
class BaseRecord |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Database por defecto usa default. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected static $database = 'default'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* PK por defecto, si no existe mira en metadata. |
36
|
|
|
* |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
protected static $pk = 'id'; |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Values of columns |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
protected $values = []; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Constructor. |
50
|
|
|
* |
51
|
|
|
* @param array $data |
52
|
|
|
*/ |
53
|
|
|
public function __construct(array $data = []) |
54
|
|
|
{ |
55
|
|
|
$this->dump($data); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function __set($key, $value){ |
59
|
|
|
$this->values[$key] = $value; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function __get($key){ |
63
|
|
|
return isset($this->values[$key]) ? $this->values[$key]: NULL; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get the Primary Key value for the object |
68
|
|
|
* @return mixed |
69
|
|
|
*/ |
70
|
|
|
public function pk(){ |
71
|
|
|
$pk = static::getPK(); |
72
|
|
|
return $this->$pk; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Cargar datos al objeto. |
77
|
|
|
* |
78
|
|
|
* @param array $data |
79
|
|
|
*/ |
80
|
|
|
public function dump(array $data = []) |
81
|
|
|
{ |
82
|
|
|
foreach ($data as $k => $v) { |
83
|
|
|
$this->$k = $v; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Alias de los campos. |
89
|
|
|
* |
90
|
|
|
* @return array |
91
|
|
|
*/ |
92
|
|
|
public static function alias() |
93
|
|
|
{ |
94
|
|
|
return []; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Verifica que PK este seteado. |
99
|
|
|
* |
100
|
|
|
* @throw \KumbiaException |
101
|
|
|
*/ |
102
|
|
|
protected function hasPK() |
103
|
|
|
{ |
104
|
|
|
$pk = static::getPK(); |
105
|
|
|
if (empty($this->$pk)) { |
106
|
|
|
throw new \KumbiaException(__('No se ha especificado valor para la clave primaria')); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Get the name of the primary key |
112
|
|
|
* |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
|
|
public static function getPK() |
116
|
|
|
{ |
117
|
|
|
if (static::$pk) { |
118
|
|
|
return static::$pk; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return self::metadata()->getPK(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Obtiene nombre de tabla en la bd. |
126
|
|
|
* |
127
|
|
|
* @return string smallcase del nombre de la clase |
128
|
|
|
*/ |
129
|
|
|
public static function getTable() |
130
|
|
|
{ |
131
|
|
|
return strtolower(preg_replace('/([A-Z])/', '_\\1', lcfirst(\get_called_class()))); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Obtiene el schema al que pertenece. |
136
|
|
|
* |
137
|
|
|
* @return string |
138
|
|
|
*/ |
139
|
|
|
public static function getSchema() |
140
|
|
|
{ |
141
|
|
|
return ''; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Obtiene la combinación de esquema y tabla. |
146
|
|
|
* |
147
|
|
|
* @return string |
148
|
|
|
*/ |
149
|
|
|
public static function getSource() |
150
|
|
|
{ |
151
|
|
|
$source = static::getTable(); |
152
|
|
|
if ($schema = static::getSchema()) { |
153
|
|
|
$source = "$schema.$source"; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return $source; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Obtiene la conexión que se utilizará (contenidas en databases.php). |
161
|
|
|
* |
162
|
|
|
* @return string |
163
|
|
|
*/ |
164
|
|
|
public static function getDatabase() |
165
|
|
|
{ |
166
|
|
|
return static::$database; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Obtiene metadatos. |
171
|
|
|
* |
172
|
|
|
* @return Metadata\Metadata |
173
|
|
|
*/ |
174
|
|
|
public static function metadata() |
175
|
|
|
{ |
176
|
|
|
return Metadata\Metadata::get( |
177
|
|
|
static::getDriver(), |
178
|
|
|
static::getDatabase(), |
179
|
|
|
static::getTable(), |
180
|
|
|
static::getSchema() |
181
|
|
|
); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Obtiene manejador de conexion a la base de datos. |
186
|
|
|
* |
187
|
|
|
* @param bool $force forzar nueva conexion PDO |
188
|
|
|
* |
189
|
|
|
* @return \PDO |
190
|
|
|
*/ |
191
|
|
|
protected static function dbh($force = false) |
192
|
|
|
{ |
193
|
|
|
return Db::get(static::getDatabase(), $force); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Consulta sql preparada. |
198
|
|
|
* |
199
|
|
|
* @param string $sql |
200
|
|
|
* |
201
|
|
|
* @return \PDOStatement |
202
|
|
|
* @throw \PDOException |
203
|
|
|
*/ |
204
|
|
|
public static function prepare($sql) |
205
|
|
|
{ |
206
|
|
|
$sth = self::dbh()->prepare($sql); |
207
|
|
|
$sth->setFetchMode(\PDO::FETCH_CLASS, \get_called_class()); |
208
|
|
|
|
209
|
|
|
return $sth; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Retorna el último ID insertado. |
214
|
|
|
* |
215
|
|
|
* @return ID |
216
|
|
|
*/ |
217
|
|
|
public static function lastInsertId() |
218
|
|
|
{ |
219
|
|
|
return self::dbh()->lastInsertId(); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Consulta sql. |
224
|
|
|
* |
225
|
|
|
* @param string $sql |
226
|
|
|
* |
227
|
|
|
* @return \PDOStatement |
228
|
|
|
* @throw PDOException |
229
|
|
|
*/ |
230
|
|
|
public static function sql($sql) |
231
|
|
|
{ |
232
|
|
|
$sth = self::dbh()->query($sql); |
233
|
|
|
$sth->setFetchMode(\PDO::FETCH_CLASS, \get_called_class()); |
234
|
|
|
|
235
|
|
|
return $sth; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Ejecuta consulta sql. |
240
|
|
|
* |
241
|
|
|
* @param string $sql |
242
|
|
|
* @param array | string $values valores |
243
|
|
|
* |
244
|
|
|
* @return PDOStatement |
245
|
|
|
*/ |
246
|
|
|
public static function query($sql, $values = null) |
247
|
|
|
{ |
248
|
|
|
if (func_num_args() === 1) { |
249
|
|
|
return self::sql($sql); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
$sth = self::prepare($sql); |
253
|
|
|
if (!is_array($values)) { |
254
|
|
|
$values = \array_slice(\func_get_args(), 1); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
return $sth->execute($values) ? $sth : false; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Verifica si existe el registro. |
262
|
|
|
* |
263
|
|
|
* @param string $pk valor para clave primaria |
264
|
|
|
* |
265
|
|
|
* @return bool |
266
|
|
|
*/ |
267
|
|
View Code Duplication |
public static function exists($pk) |
|
|
|
|
268
|
|
|
{ |
269
|
|
|
$source = static::getSource(); |
270
|
|
|
$pkField = static::getPK(); |
271
|
|
|
|
272
|
|
|
return self::query("SELECT COUNT(*) AS count FROM $source WHERE $pkField = ?", $pk)->fetch()->count > 0; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Paginar consulta sql. |
277
|
|
|
* |
278
|
|
|
* @param string $sql consulta select sql |
279
|
|
|
* @param int $page numero de pagina |
280
|
|
|
* @param int $perPage cantidad de items por pagina |
281
|
|
|
* @param array $values valores |
282
|
|
|
* |
283
|
|
|
* @return Paginator |
284
|
|
|
*/ |
285
|
|
|
public static function paginateQuery($sql, $page, $perPage, $values = []) |
286
|
|
|
{ |
287
|
|
|
return new Paginator(\get_called_class(), $sql, (int) $page, (int) $perPage, $values); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Devuelve el nombre del drive PDO utilizado. |
292
|
|
|
* |
293
|
|
|
* @return string |
294
|
|
|
*/ |
295
|
|
|
public static function getDriver() |
296
|
|
|
{ |
297
|
|
|
return self::dbh()->getAttribute(\PDO::ATTR_DRIVER_NAME); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* Comienza una trasacción. |
302
|
|
|
* |
303
|
|
|
* @return bool |
304
|
|
|
*/ |
305
|
|
|
public static function begin() |
306
|
|
|
{ |
307
|
|
|
return self::dbh()->beginTransaction(); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Da marcha atrás a una trasacción. |
312
|
|
|
* |
313
|
|
|
* @return bool |
314
|
|
|
*/ |
315
|
|
|
public static function rollback() |
316
|
|
|
{ |
317
|
|
|
return self::dbh()->rollBack(); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* Realiza el commit de una trasacción. |
322
|
|
|
* |
323
|
|
|
* @return bool |
324
|
|
|
*/ |
325
|
|
|
public static function commit() |
326
|
|
|
{ |
327
|
|
|
return self::dbh()->commit(); |
328
|
|
|
} |
329
|
|
|
} |
330
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.