Ver Mensaje Individual
  #7 (permalink)  
Antiguo 15/08/2013, 18:00
Avatar de lordalucardmx
lordalucardmx
 
Fecha de Ingreso: junio-2012
Ubicación: Cuernavaca
Mensajes: 38
Antigüedad: 11 años, 10 meses
Puntos: 1
Respuesta: Acceso a registros de MySQL de acuerdo a su nivel de usuario

Ahora me surge otro problema en cuanto a herencia de métodos y atributos de clases:
Teno una clase Log que guarda excepciones en un archivo y la clase Database hereda todo sobre él, y a su vez creo un objeto sesion que toma los métodos de Database para ejecutar las consultas, y éste me guarda las excepciones en el archivo log, pero en una validación que tengo no me muestra el mensaje de error en la página.

Database.class.php
Código PHP:
Ver original
  1. <?php
  2. require_once('core/classes/log.class.php');
  3. class Database extends Log{
  4.  
  5.     private static $host      = ''/*DB_HOST*/;
  6.     private static $user      = ''/*DB_USER*/;
  7.     private static $pass      = ''/*DB_PASS*/;
  8.     protected $dbname    = ''/*DB_NAME*/;
  9.     protected $stmt;
  10.     protected $charset   = '';
  11.     private $db;
  12.  
  13.     public function __construct($host, $user, $pass, $dbname, $charset = 'utf8'){
  14.         self::$host = $host;
  15.         self::$user = $user;
  16.         self::$pass = $pass;
  17.         $this->dbname = $dbname;
  18.         $this->charset = $charset;
  19.         // Set DSN
  20.         $dsn = 'mysql:host=' . self::$host . ';dbname=' . $this->dbname . ';charset=' . $this->charset;
  21.         // Set options
  22.         $options = array(
  23.             PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
  24.             PDO::ATTR_PERSISTENT    => true,
  25.             PDO::ATTR_ERRMODE       => PDO::ERRMODE_WARNING,
  26.             PDO::ATTR_ERRMODE       => PDO::ERRMODE_EXCEPTION
  27.         );
  28.         // Create a new PDO instanace
  29.         try{
  30.             $this->db = new PDO($dsn, self::$user,self::$pass, $options);
  31.         }
  32.         // Catch any errors
  33.         catch(PDOException $e){
  34.             $this->elog($e);
  35.             $this->setException($e);
  36.         }
  37.     }
  38.  
  39.     private function close(){
  40.         $this->db = NULL;
  41.     }
  42.  
  43.     protected function getList($query, $flag){
  44.             $this->query($query);
  45.             $rows = $this->resultset($flag);
  46.             return $rows;
  47.     }
  48.  
  49.     protected function query($query){
  50.         $this->stmt = $this->db->prepare($query);
  51.     }
  52.  
  53.     protected function bind($param, $value, $type = null){
  54.         if (is_null($type)) {
  55.             switch (true) {
  56.                 case is_int($value):
  57.                     $type = PDO::PARAM_INT;
  58.                     break;
  59.                 case is_bool($value):
  60.                     $type = PDO::PARAM_BOOL;
  61.                     break;
  62.                 case is_null($value):
  63.                     $type = PDO::PARAM_NULL;
  64.                     break;
  65.                 default:
  66.                     $type = PDO::PARAM_STR;
  67.             }
  68.         }
  69.         $this->stmt->bindValue($param, $value, $type);
  70.     }
  71.  
  72.     protected function execute(){
  73.         try{
  74.             return $this->stmt->execute();
  75.         }catch(PDOException $e){
  76.             $this->elog($e);
  77.             $this->setException($e);
  78.         }
  79.     }
  80.  
  81.     protected function fetchColumn(){
  82.         try{
  83.             $this->execute();
  84.             return $this->stmt->fetchColumn();
  85.         }catch(PDOException $e){
  86.             $this->elog($e);
  87.             $this->setException($e);
  88.         }
  89.     }
  90.  
  91.     protected function resultset($flag = false){
  92.         try{
  93.             $this->execute();
  94.             if($flag === true){
  95.                 return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
  96.             }else{
  97.                 return $this->stmt->fetchAll();
  98.             }
  99.         }catch(PDOException $e){
  100.             $this->elog($e);
  101.             $this->setException($e);
  102.         }
  103.     }
  104.  
  105.     protected function single(){
  106.         try{
  107.             $this->execute();
  108.             return $this->stmt->fetch(PDO::FETCH_ASSOC);
  109.         }catch(PDOException $e){
  110.             $this->elog($e);
  111.             $this->setException($e);
  112.         }
  113.     }
  114.  
  115.     protected function rowCount(){
  116.         return $this->stmt->rowCount();
  117.     }
  118.  
  119.     protected function lastInsertId(){
  120.         return $this->db->lastInsertId();
  121.     }
  122.  
  123.     protected function beginTransaction(){
  124.         return $this->db->beginTransaction();
  125.     }
  126.  
  127.     protected function endTransaction(){
  128.         return $this->db->commit();
  129.     }
  130.  
  131.     protected function cancelTransaction(){
  132.         return $this->db->rollBack();
  133.     }
  134.  
  135.     protected function debugDumpParams(){
  136.         return $this->stmt->debugDumpParams();        
  137.     }
  138.  
  139. }
  140.  
  141. ?>

Log.class.php

Código PHP:
Ver original
  1. <?php
  2. abstract class Log{
  3.  
  4.     protected $statusEx = false;
  5.     protected $getCode;
  6.     protected $getMessage;
  7.     protected $msj = array();
  8.  
  9.     protected function eLog(Exception $e){
  10.         $fo = fopen(dirname(dirname(dirname(__FILE__)))."\logs\\".date('ymd').'.log','a');
  11.         fwrite($fo,"[".date("r")."] " .$e->getTraceAsString(). "\r\n" . $e->getMessage());
  12.         fclose($fo);
  13.     }
  14.  
  15.     protected function setException(Exception $e){
  16.         $this->getCode = $e->getCode();
  17.         $this->getMessage = $e->getMessage();
  18.         $this->statusEx = true;
  19.     }
  20.  
  21.     public function getException(){
  22.         $this->msj[] = '<div class="alert alert-error">
  23.                Error número: ' .$this->getCode. '</br>
  24.                Mensaje: '.$this->getMessage.'</div></br>';
  25.         return $this->msj;
  26.     }
  27.  
  28.     public function getStatus(){
  29.         return $this->statusEx;
  30.     }
  31.  
  32. }
  33. ?>

init.php

Código PHP:
Ver original
  1. require 'classes/sesion.class.php';
  2. require 'classes/general.php';
  3. require 'classes/bcrypt.php';
  4. $sesion     = new Sesion();


sesion.class.php

Código PHP:
Ver original
  1. <?php
  2. require_once('core/connect/database.class.php');
  3. class Sesion extends Database{
  4.  
  5.     public function __construct(){
  6.         $this->db = new Database('127.0.0.1', 'root', '', 'lar');
  7.     }  
  8. }
  9.  
  10. ?>

en la página login.php tengo esto:

Código PHP:
Ver original
  1. if($sesion->getStatus() == true){
  2.             echo implode($sesion->getException());
  3.         }

Antes si me mostraba el mensaje, pero ahora ya no, y lo necesito para controlar lo mejor posible los errores mientras termino el proyecto.