Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?
- class DAO{
- ### atributos / objetos para encapsulamento ###
- private $id;
- protected $ultimoId;
- ### atributos / objetos usar no CRUD ###
- protected $tabela;
- protected $campos;
- protected $valores;
- protected $where;
- protected $like;
- protected $asc;
- protected $desc;
- public $start;
- public $limit;
- public $total;
- ### atributos / objetos para conexão ###
- private static $conn;
- const DB_HOST = 'localhost';
- const DB_NOME = 'painel';
- const DB_PORT = '3306';
- const DB_USER = 'root';
- const DB_PASS = '';
- const TIPO_CON = 'mysql';
- ### atributos / objetos para manipular a sessão ###
- protected $session = true;
- public $dados;
- public static function getDAO(){
- if(self::$conn instanceof PDO){
- return self::$conn;
- }else{
- self::$conn = new PDO(strtolower(self::TIPO_CON).":host=".self::DB_HOST.";port=".self::DB_PORT.";dbname=".
- self::DB_NOME, self::DB_USER, self::DB_PASS);
- return self::$conn;
- }
- }
- public function getResultado($sql='', $stm_arr=array()){
- if(!empty($sql)){
- $conn = DAO::getDAO();
- $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
- $stm = $conn->prepare($sql);
- $stm->execute($stm_arr);
- $stm->debugDumpParams();
- $result = $stm->fetchAll(PDO::FETCH_ASSOC);
- $this->total = $stm->rowCount();
- #print_r($this->total);
- if(!empty($result)){
- return $result;
- }
- return array();
- }else{
- return array();
- }
- }
- public function ExecutaSQL($sql=''){
- if(!empty($sql)){
- $conn = DAO::getDAO();
- $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
- $stm = $conn->prepare($sql);
- $stm->debugDumpParams();
- $stm->execute();
- $this->ultimoId = (int)DAO::$conn->lastInsertId();
- if(!$conn->errorCode()){
- return true;
- }
- return false;
- }else{
- return false;
- }
- }
- protected function select(){
- $padrao = (!empty($this->desc) ? "ORDER BY {$this->desc} DESC" : "ORDER BY {$this->asc} ASC");
- $where = (!empty($this->where) ? "WHERE {$this->where}" : "" );
- $limit = (!empty($this->limit) ? "LIMIT {$this->limit}" : "" );
- $like = (!empty($this->like) ? "LIKE '%{$this->like}%'" : "" );
- return self::getResultado("SELECT {$this->campos} FROM {$this->tabela} {$like} {$where} {$padrao} {$limit}");
- }
- protected function insert(){
- return self::ExecutaSQL("INSERT INTO {$this->tabela} ({$this->campos}) VALUES ({$this->valores})");
- }
- protected function update(){
- return self::ExecutaSQL("UPDATE {$this->tabela} SET {$this->campos} WHERE id = ".self::getId()."");
- }
- protected function delete(){
- return self::ExecutaSQL("DELETE FROM {$this->tabela} WHERE id = ".self::getId()."");
- }
- /*protected function sessionUser($array){
- if(!empty($this->session)){
- if($this->session != false){
- session_start();
- $_SESSION[$array] = $this->dados;
- }else{
- unset($_SESSION[$array]);
- session_destroy();
- }
- }
- }*/
- protected function setUltimoId($ultimoId){
- $this->ultimoId = $ultimoId;
- }
- protected function getUltimoId(){
- return $this->ultimoId;
- }
- protected function setId($id){
- $this->id = (int)$id;
- }
- protected function getId(){
- return intval($this->id);
- }
- public function getData($data){
- return implode('/', array_reverse(explode('-',$data)));
- }
- public function getMoney($valor){
- return 'R$' . number_format($valor, 2, ',', '.');
- }
- }
- ?>
Add Comment
Please, Sign In to add comment