316
332
public function sql ($sql, $params = NULL, $fetchMode = NULL)
318
// Obtiene una instancia del adaptador
319
$adapter = DbAdapter::factory($this->_connection);
322
// Prepara la consulta
323
$this->_resultSet = $adapter->prepare($sql);
335
// Obtiene una instancia del adaptador y prepara la consulta
336
$this->_resultSet = DbAdapter::factory($this->_connection)->prepare($sql);
325
338
// Indica el modo de obtener los datos en el ResultSet
326
339
$this->_fetchMode($fetchMode);
351
364
if ($this->_schema) {
352
365
$dbQuery->schema($this->_schema);
355
// Obtiene una instancia del adaptador
356
$adapter = DbAdapter::factory($this->_connection);
359
// Prepara la consulta
360
$this->_resultSet = $adapter->prepareDbQuery($dbQuery);
369
// Obtiene una instancia del adaptador y prepara la consulta
370
$this->_resultSet = DbAdapter::factory($this->_connection)->prepareDbQuery($dbQuery);
362
372
// Indica el modo de obtener los datos en el ResultSet
363
373
$this->_fetchMode($fetchMode);
436
446
public function findBy($column, $value, $fetchMode = NULL)
438
$this->get()->where("$column = :value")->bind(array('value' => $value));
448
$this->get()->where("$column = :value")->bindValue('value', $value);
439
449
return $this->first($fetchMode);
450
460
public function findAllBy($column, $value, $fetchMode = NULL)
452
$this->get()->where("$column = :value")->bind(array('value' => $value));
462
$this->get()->where("$column = :value")->bindValue('value', $value);
453
463
return $this->find($fetchMode);
467
* Obtiene la clave primaria
471
public function getPK()
473
return DbAdapter::factory($this->_connection)->describe($this->getTable(), $this->_schema)->getPK();
457
477
* Buscar por medio de la clave primaria
459
479
* @param string $value
463
483
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);
485
return $this->findBy($this->getPK(), $value, $fetchMode);
477
494
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
498
// Itera en cada atributo
486
foreach($metadata->getAttributesList() as $attr) {
499
foreach(DbAdapter::factory($this->_connection)
500
->describe($this->getTable(), $this->_schema)
501
->getAttributesList() as $attr) {
487
503
if(property_exists($this, $attr)) {
488
504
if($this->$attr === '') {
489
505
$data[$attr] = NULL;
510
526
if (is_array($data)) {
511
527
$this->dump($data);
530
// @see ActiveRecordValidator
531
require_once CORE_PATH . 'libs/ActiveRecord/active_record2/active_record_validator.php';
533
// Ejecuta la validacion
534
if(ActiveRecordValidator::validateOnCreate($this) === FALSE) {
514
538
// Callback antes de crear
515
539
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
543
// Nuevo contenedor de consulta
528
544
$dbQuery = new DbQuery();
622
* Establece condicion de busqueda con clave primaria
624
* @param DbQuery $dbQuery
626
protected function _wherePK($dbQuery)
628
// Obtiene la clave primaria
629
$pk = $this->getPK();
631
// Si es clave primaria compuesta
634
if(!isset($this->$k)) {
635
throw new KumbiaException("Debe definir valor para la columna $k de la clave primaria");
638
$dbQuery->where("$k = :pk_$k")->bindValue("pk_$k", $this->$k);
641
if(!isset($this->$pk)) {
642
throw new KumbiaException("Debe definir valor para la clave primaria");
645
$dbQuery->where("$pk = :pk_$pk")->bindValue("pk_$pk", $this->$pk);
606
650
* Verifica si esta persistente en la BD el objeto actual en la bd
608
652
* @return boolean
610
654
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));
656
// Objeto de consulta
657
$dbQuery = $this->get();
659
// Establece condicion de busqueda con clave primaria
660
$this->_wherePK($dbQuery);
624
662
return $this->existsOne();
666
* Realiza un update del registro sobre la tabla
668
* @param array $data información a ser guardada
671
public function update($data = NULL)
673
// Si es un array, se cargan los atributos en el objeto
674
if (is_array($data)) {
678
// @see ActiveRecordValidator
679
require_once CORE_PATH . 'libs/ActiveRecord/active_record2/active_record_validator.php';
681
// Ejecuta la validacion
682
if(ActiveRecordValidator::validateOnUpdate($this) === FALSE) {
686
// Callback antes de actualizar
687
if($this->_beforeUpdate() === FALSE) {
691
// Si no existe el registro
692
if(!$this->exists()) {
696
// Objeto de consulta
697
$dbQuery = new DbQuery();
698
// Establece condicion de busqueda con clave primaria
699
$this->_wherePK($dbQuery);
701
// Ejecuta la consulta con el query utilizado para el exists
702
if($this->query($dbQuery->update($this->_getTableValues()))) {
703
// Callback despues de actualizar
704
$this->_afterUpdate();