Advertisement
Guest User

Untitled

a guest
Aug 26th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * CRUD Model - Ocrend Framework
  5. * Ejemplo de uso correcto para un CRUD usando rutas y la api rest
  6. */
  7.  
  8. final class Crud extends Models implements OCREND {
  9.  
  10. public function __construct() {
  11. parent::__construct();
  12. }
  13.  
  14. final public function errores(array $data) {
  15. try {
  16. if(Func::emp($data['ejemplo'])) {
  17. throw new Exception('Error campo vacío.');
  18. }
  19.  
  20. return false;
  21. } catch(Exception $e) {
  22. return array('success' => 0, 'message' => $e->getMessage());
  23. }
  24. }
  25.  
  26. final public function crear(array $data) : array {
  27. $error = $this->errores($data);
  28. if(false !== $error) {
  29. return $error;
  30. }
  31.  
  32. $i = array(
  33. 'ejemplo' => $data['ejemplo']
  34. );
  35. # ó también: $i['ejemplo'] = $data['ejemplo'];
  36. $this->db->insert('tabla',$i);
  37.  
  38. return array('success' => 1, 'message' => 'Éxito');
  39. }
  40.  
  41. final public function leer(bool $multi = true) {
  42. if($multi) {
  43. return $this->db->select('*','tabla');
  44. }
  45.  
  46. return $this->db->select('*','tabla',"id='$this->id'");
  47. }
  48.  
  49. final public function actualizar() : array {
  50. $error = $this->errores($data);
  51. if(false !== $error) {
  52. return $error;
  53. }
  54.  
  55. $i = array(
  56. 'ejemplo' => $data['ejemplo']
  57. );
  58. # ó también: $i['ejemplo'] = $data['ejemplo'];
  59.  
  60. $this->id = $this->db->scape($data['id']); # porque desde la api rest no se define $this->id ya que no existe la ruta.
  61. $this->db->update('tabla',$i,"id='$this->id'",'LIMIT 1');
  62.  
  63. return array('success' => 1, 'message' => 'Éxito');
  64. }
  65.  
  66. final public function borrar() {
  67. $this->db->delete('tabla',"id='$this->id'");
  68. Func::redir(URL . 'crud/');
  69. }
  70.  
  71. public function __destruct() {
  72. parent::__destruct();
  73. }
  74.  
  75. }
  76.  
  77. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement