Ver Mensaje Individual
  #10 (permalink)  
Antiguo 10/01/2016, 22:26
Avatar de detective_jd
detective_jd
 
Fecha de Ingreso: abril-2011
Ubicación: Salto
Mensajes: 437
Antigüedad: 13 años
Puntos: 6
Respuesta: Evitar eliminar registros con php

Hola x_atrix, adapté lo que me dijiste y funciona, gracias:

Rol.php

Código PHP:
Ver original
  1. <?php
  2. namespace Clases;
  3. use \Model\RolModel;
  4. class Rol
  5. {
  6.     private $id;
  7.     private $nombre;
  8.     private $mod;
  9.     function getId() {
  10.         return $this->id;
  11.     }
  12.     function getNombre() {
  13.         return $this->nombre;
  14.     }    
  15.     function setNombre($nombre) {
  16.         $this->nombre = strtoupper($nombre);
  17.     }
  18.     function __construct($xid, $xnombre) {
  19.         $this->id = $xid;
  20.         $this->nombre = strtoupper($xnombre);
  21.     }
  22.     public function equals(Rol $obj){
  23.         return $this->nombre == $obj->nombre;                
  24.     }
  25.     public function save(){
  26.         $this->mod = new RolModel();
  27.         return ($this->id == 0) ? $this->mod->guardame($this) : $this->mod->modificame($this);
  28.     }
  29.     public function del(){
  30.         $this->mod = new RolModel();
  31.         return $this->mod->eliminame($this);
  32.     }
  33. }

RolesController.php

Código PHP:
Ver original
  1. <?php
  2. namespace Controller;
  3. use \App\Controller;
  4. use \App\Session;
  5. use \Model\RolModel;
  6. use \Clases\Rol;
  7. class RolesController extends Controller
  8. {
  9.     private $modelo;
  10.     function __construct() {
  11.         parent::__construct();
  12.         $this->modelo= new RolModel();
  13.     }
  14.     public function index(){
  15.         if($this->checkUser()){
  16.             $this->redirect(array("index.php"),array(
  17.                 "roles" => $this->modelo->obtenerTodos()
  18.             ));
  19.         }  
  20.     }
  21.     public function add(){
  22.         if($this->checkUser()){
  23.             if (isset($_POST['btnaceptar'])) {
  24.                 if($this->checkDates()) {                
  25.                     $rol = new Rol(0, $_POST['txtnom']);
  26.                     //$id= $this->modelo->guardame($rol);
  27.                     $id = $rol->save();
  28.                     Session::set("msg",(isset($id)) ? "Rol Creado" : Session::get('msg'));
  29.                     header("Location:index.php?c=roles&a=index");
  30.                     exit();
  31.                 }
  32.             }
  33.             $this->redirect(array('add.php'));
  34.         }
  35.     }  
  36.     public function edit(){        
  37.         if($this->checkUser()){
  38.             Session::set("id",$_GET['p']);
  39.             if (Session::get('id')!=null && isset($_POST['btnaceptar'])){                            
  40.                 if($this->checkDates()) {                
  41.                     $rol= new Rol($_POST['hid'],$_POST['txtnom']);
  42.                     //$id= $this->modelo->modificame($rol);
  43.                     $id = $rol->save();
  44.                     Session::set("msg",(isset($id)) ? "Rol Editado" : Session::get('msg'));
  45.                     header("Location:index.php?c=roles&a=index");
  46.                     exit();
  47.                 }
  48.             }
  49.             $this->redirect(array('edit.php'),array(
  50.                 "rol" => $this->modelo->obtenerPorId(Session::get('id'))
  51.             ));
  52.         }        
  53.     }
  54.     public function delete(){
  55.         if($this->checkUser()){
  56.             if (isset($_GET['p'])){
  57.                 $rol = $this->modelo->obtenerPorId($_GET['p']);
  58.                 //$id= $this->modelo->eliminame($rol);
  59.                 $id= $rol->del();
  60.                 Session::set("msg", (isset($id)) ? "Rol Borrado" : "No se pudo borrar el rol");
  61.                 header("Location:index.php?c=roles&a=index");
  62.             }                          
  63.         }
  64.     }
  65.     private function checkDates(){
  66.         if(empty($_POST['txtnom'])){
  67.             Session::set("msg","Ingrese los datos obligatorios (*) para continuar.");
  68.             return false;
  69.         }
  70.         else{
  71.             return true;
  72.         }
  73.     }
  74.     private function checkUser(){
  75.         if(Session::get("log_in")!= null and Session::get("log_in")->getRol()->getNombre() == "ADMIN"){
  76.             return true;
  77.         }
  78.         else {
  79.             Session::set("msg","Debe ser administrador para acceder.");
  80.             $this->redirect(array('Main','index.php'));
  81.         }
  82.     }
  83. }

Pero tuve el problema que en la clase Rol no podía instanciar el modelo en el constructor xq si lo hacía parte del sistema me dejaba de funcionar, ¿porque será?
En el controlador te pregunto ¿cómo hacer algo parecido a lo que me dijiste pero con obtenerTodos y obtenerPorId? porque según lo puesto estaríamos haciendo que la clase persista sus propios datos, probé agregarle a la clase Rol otro constructor vacío para instanciar el modelo pero php no deja hacer más de un controlador ¿que debo hacer?

Saludos y espero respuestas.