class BD { static $server = "localhost"; static $user = "root"; static $password = ""; static $database = "sakila"; private $table; private $idField; private $fields; private $showFields; static private $conn; public function __construct($table, $idField, $fields = "", $showFields = "") { $this->table = $table; $this->idField = $idField; $this->fields = $fields; $this->showFields = $showFields; self::conectar(); } static function conectar() { try { self::$conn = new PDO("mysql:host=" . self::$server . ";dbname=" . self::$database, self::$user, self::$password, [PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"]); self::$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch (Exception $ex) { echo $ex->getMessage(); } } function __get($name) { if (property_exists($this, $name)) { return $this->$name; } } function __set($name, $value) { if (property_exists($this, $name) && !empty($value)) { $this->$name = $value; } else { throw new Exception("Error: datos incorrectos"); } } function getAll($condicion = "") { $where = ""; if (!empty($condicion)) { //Aquí tendré que hacer algo!!! $where = " where 1=1 "; foreach ($condicion as $clave => $valor) { $where .= " and " . $clave . " = '" . $valor . "' "; } } $res = self::$conn->query("select * from " . $this->table . $where); return $res->fetchAll(PDO::FETCH_ASSOC); } function getAllPrepare($condicion = []) { $where = ""; if (!empty($condicion)) { $where = " where ". join(" and ", array_map(function($v) { return $v . "=:" . $v; }, array_keys($condicion))); } $st = self::$conn->prepare("select * from " . $this->table . $where); $st->execute($condicion); return $st->fetchAll(PDO::FETCH_ASSOC); } /** * Esta función nos devuelve el elemento de la tabla que tenga este id * @param int $id El id de la fila */ function getById($id) { $res = self::$conn->query("select * from " . $this->table . " where " . $this->idField . "=" . $id); return $res->fetch(); } /** * Elimina el registro que tenga el id que le pasamos * @param int $id */ function deleteById($id) { try { self::$conn->exec("delete from " . $this->table . " where " . $this->idField . "=" . $id); } catch (Exception $ex) { echo $ex->getMessage(); } } /** * Esta función toma como parámetro un array asociativo y nos inserta en la tabla * un registro donde la clave del array hace referencia al campo de la tabla y * el valor del array al valor de la tabla. * ejemplo para la tabla actor: insert(['first_name'=>'Ana','last_name'=>'Pi']) * @param type $valores */ function insert($valores) { try { $campos = join(",", array_keys($valores)); $parametros = ":" . join(",:", array_keys($valores)); $sql = "insert into " . $this->table . "($campos) values ($parametros)"; $st = self::$conn->prepare($sql); $st->execute($valores); } catch (Exception $ex) { echo $ex->getMessage(); } } /** * Modifica el elemento de la base de datos con el id que pasamos * Con los valores del array asociativo * @param int $id Id del elemento a modificar * @param array $valores Array asociativo con los valores a modificar */ function update($id, $valores) { try { //Easy way $res = []; foreach ($valores as $clave => $valor) { $res[] = $clave . "=:" . $clave; } $campos = join(",", $res); //Hack way $campos = join(",", array_map(function($v) { return $v . "=:" . $v; }, array_keys($valores))); $sql = "update " . $this->table . " set " . $campos . " where " . $this->idField . " = " . $id; $st = self::$conn->prepare($sql); $st->execute($valores); } catch (Exception $ex) { echo $ex->getMessage(); } } }