Skip to content

Instantly share code, notes, and snippets.

@demonio
Last active August 29, 2020 22:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save demonio/55ec68c51e973b7b280d322a06c76644 to your computer and use it in GitHub Desktop.
Save demonio/55ec68c51e973b7b280d322a06c76644 to your computer and use it in GitHub Desktop.
Un mini AR para empezar a hacer cosas entre KumbiaPHP y MsSQL
<?php
/**
*/
class Myar
{
/**
* STEP 1
*/
public function connect()
{
$config = require(APP_PATH.'config/databases.php');
$config = $config['default'];
$this->pdo = new PDO($config['dsn'], $config['username'], $config['password']);
}
/**
* STEP 2
*/
public function query($sql, $values=NULL)
{
$this->connect();
$this->query = $this->pdo->prepare($sql);
if ( ! is_array($values) )
{
$values = array_slice(func_get_args(), 1);
}
$this->query->execute($values);
#_::d($this->query);
if ($this->query)
{
if ( preg_match('/^ALTER TABLE/i', $sql) ) $_SESSION['toasts'][] = "Tabla alterada";
else if ( preg_match('/^CREATE TABLE/i', $sql) ) $_SESSION['toasts'][] = "Tabla creada";
else if ( preg_match('/^DELETE/i', $sql) ) $_SESSION['toasts'][] = "Registro borrado";
else if ( preg_match('/^INSERT/i', $sql) ) $_SESSION['toasts'][] = "Registro insertado";
else if ( preg_match('/^UPDATE/i', $sql) ) $_SESSION['toasts'][] = "Registro actualizado";
}
else
{
$error = $this->pdo->errorInfo();
_::d($error);
}
}
/**
* STEP 3a
*/
public function fetch($sql, $values=NULL, $test=0)
{
$this->query($sql, $values);
$this->rows = $this->query->fetch(PDO::FETCH_OBJ);
if ($test) _::d($this->rows);
if ($this->rows) return $this->rows;
}
/**
* STEP 3b
*/
public function fetchAll($sql, $values=NULL, $test=0)
{
$this->query($sql, $values);
$this->rows = $this->query->fetchAll(PDO::FETCH_OBJ);
if ($test) _::d($this->rows);
if ($this->rows) return $this->rows;
}
/**
* STEP 4 ONLY FOR FILL
*/
public function columns($table='')
{
if ( ! $table ) $table = strtolower( preg_replace( '/([A-Z])/', "_\\1", lcfirst( get_called_class() ) ) );
$sql = "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME=?";
$this->query($sql, $table);
$columns = $this->query->fetchAll(PDO::FETCH_OBJ);
if ( ! $columns) return '';
$o = new stdClass;
foreach($columns as $column) $o->{$column->COLUMN_NAME} = '';
return array(0=>$o);
}
/**
*/
public function combo($table, $field='')
{
$field = empty($field) ? "nombre_$table": $field;
$sql = "SELECT id_$table AS id, $field AS nombre FROM $table WHERE eliminado IS NULL";
return $this->fetchAll($sql);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment