41
41
* Obtener datos cargados en objeto del Modelo
44
const FETCH_MODEL = 1;
44
const FETCH_MODEL = 'model';
47
47
* Obtener datos cargados en objeto
50
const FETCH_OBJ = 'obj';
53
53
* Obtener datos cargados en array
56
const FETCH_ARRAY = 3;
56
const FETCH_ARRAY = 'array';
59
59
* Conexion a base datos que se utilizara
63
protected $_connection = null;
63
protected $_connection = NULL;
66
66
* Tabla origen de datos
70
protected $_table = null;
70
protected $_table = NULL;
77
protected $_schema = null;
77
protected $_schema = NULL;
80
80
* Objeto DbQuery para implementar chain
191
207
* Indica el modo de obtener datos al ResultSet actual
194
protected function _fetchMode()
210
protected function _fetchMode($fetchMode = NULL)
196
switch ($this->_fetchMode) {
212
// Si no se especifica toma el por defecto
214
$fetchMode = $this->_fetchMode;
217
switch ($fetchMode) {
197
218
// Obtener instancias del mismo modelo
198
219
case self::FETCH_MODEL:
220
// Instancias de un nuevo modelo, por lo tanto libre de los atributos de la instancia actual
199
221
$this->_resultSet->setFetchMode(PDO::FETCH_INTO, new self());
261
283
return $this->_schema;
289
* @param string $conn
290
* @return ActiveRecord
292
public function setConnection($conn)
294
$this->_connection = $conn;
299
* Obtiene la conexion
303
public function getConnection()
305
return $this->_connection;
265
309
* Ejecuta una setencia SQL aplicando Prepared Statement
267
311
* @param string $sql Setencia SQL
268
312
* @param array $params parametros que seran enlazados al SQL
313
* @param string $fetchMode
269
314
* @return ActiveRecord
271
public function sql ($sql, $params = NULL)
316
public function sql ($sql, $params = NULL, $fetchMode = NULL)
273
318
// Obtiene una instancia del adaptador
274
319
$adapter = DbAdapter::factory($this->_connection);
295
340
* Ejecuta una consulta de dbQuery
297
342
* @param DbQuery $dbQuery Objeto de consulta
343
* @param string $fetchMode
298
344
* @return ActiveRecord
300
public function query($dbQuery)
346
public function query($dbQuery, $fetchMode = NULL)
302
348
$dbQuery->table($this->getTable());
342
388
* Efectua una busqueda
390
* @param string $fetchMode
344
391
* @return ActiveRecord
346
public function find ()
393
public function find ($fetchMode = NULL)
348
395
if (! $this->_dbQuery) {
351
return $this->query($this->_dbQuery->select());
398
return $this->query($this->_dbQuery->select(), $fetchMode);
355
402
* Obtiene un array con los items resultantes de la busqueda
404
* @param string $fetchMode
359
public function all ()
407
public function all ($fetchMode = NULL)
361
return $this->find()->_resultSet->fetchAll();
409
return $this->find($fetchMode)->_resultSet->fetchAll();
365
413
* Obtiene el primer elemento de la busqueda
415
* @param string $fetchMode
367
416
* @return ActiveRecord
369
public function first ()
418
public function first ($fetchMode = NULL)
371
420
if (! $this->_dbQuery) {
375
424
// Realiza la busqueda y retorna el objeto ActiveRecord
376
return $this->query($this->_dbQuery->select()->limit(1)->offset(0))->_resultSet->fetch();
425
return $this->query($this->_dbQuery->select()->limit(1)->offset(0), $fetchMode)->_resultSet->fetch();
382
431
* @param string $column columna de busqueda
383
432
* @param string $value valor para la busqueda
433
* @param string $fetchMode
384
434
* @return ActiveRecord
386
public function findBy($column, $value)
436
public function findBy($column, $value, $fetchMode = NULL)
388
438
$this->get()->where("$column = :value")->bind(array('value' => $value));
389
return $this->first();
439
return $this->first($fetchMode);
395
445
* @param string $column columna de busqueda
396
446
* @param string $value valor para la busqueda
447
* @param string $fetchMode
397
448
* @return ActiveRecord
399
public function findAllBy($column, $value)
450
public function findAllBy($column, $value, $fetchMode = NULL)
401
452
$this->get()->where("$column = :value")->bind(array('value' => $value));
402
return $this->find();
453
return $this->find($fetchMode);
406
457
* Buscar por medio de la clave primaria
408
459
* @param string $value
410
public function findByPK($value)
412
// Obtiene una instancia del adaptador
413
$adapter = DbAdapter::factory($this->_connection);
414
$metadata = $adapter->describe($this->getTable(), $this->_schema);
416
return $this->findBy($metadata->getPK(), $value);
460
* @param string $fetchMode
461
* @return ActiveRecord
463
public function findByPK($value, $fetchMode = NULL)
465
// Obtiene la metadata
466
$metadata = DbAdapter::factory($this->_connection)->describe($this->getTable(), $this->_schema);
468
return $this->findBy($metadata->getPK(), $value, $fetchMode);
472
* Obtiene un array de los atributos que corresponden a columnas
477
private function _getTableValues()
479
// Obtiene la metadata
480
$metadata = DbAdapter::factory($this->_connection)->describe($this->getTable(), $this->_schema);
482
// TODO: Falta el caso de la clave primaria autogenerada y completar con NULL cuando la propiedad no existe
485
// Itera en cada atributo
486
foreach($metadata->getAttributesList() as $attr) {
487
if(property_exists($this, $attr)) {
488
if($this->$attr === '') {
491
$data[$attr] = $this->$attr;
425
507
public function create ($data = NULL)
427
// Nuevo contenedor de consulta
509
// Si es un array, se cargan los atributos en el objeto
510
if (is_array($data)) {
514
// Callback antes de crear
515
if($this->_beforeCreate() === FALSE) {
519
// @see ActiveRecordValidator
520
require_once CORE_PATH . 'libs/ActiveRecord/active_record2/active_record_validator.php';
522
// Ejecuta la validacion
523
if(ActiveRecordValidator::validateOnCreate($this) === FALSE) {
527
// Nuevo contenedor de consulta
428
528
$dbQuery = new DbQuery();
430
530
// Ejecuta la consulta
431
return $this->query($dbQuery->insert($data));
531
if($this->query($dbQuery->insert($this->_getTableValues()))) {
532
// Callback despues de crear
533
$this->_afterCreate();
461
567
// Ejecuta la consulta
462
568
return $this->query($this->_dbQuery->delete());
576
public function validators()
580
* Cuenta las apariciones de filas
582
* @param string $column
585
public function count($column = '*')
587
if (! $this->_dbQuery) {
591
$this->_dbQuery->columns("COUNT($column) AS n");
592
return $this->first(self::FETCH_OBJ)->n;
596
* Verifica si existe al menos una fila con las condiciones indicadas
600
public function existsOne()
602
return $this->count() > 0;
606
* Verifica si esta persistente en la BD el objeto actual en la bd
610
public function exists()
612
// Obtiene la clave primaria
613
$metadata = DbAdapter::factory($this->_connection)->describe($this->getTable(), $this->_schema);
614
$pk = $metadata->getPK();
616
// Si no esta definido valor para clave primaria
617
if(!isset($this->$pk) || !$this->$pk) {
621
// Establece la condicion de busqueda por clave primaria
622
$this->get()->where("$pk = :$pk")->bind(array($pk => $this->$pk));
624
return $this->existsOne();