Last active
December 14, 2022 05:08
-
-
Save prinick96/b8a294d722adbc36d5d98538a5a8b2a4 to your computer and use it in GitHub Desktop.
Ejemplo de un CRUD completo, utilizando la API REST con Ocrend Framework. Con el generador sería solo cuestión de escribir "python gen.py crud Modulo"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ASUMIMOS QUE ESTÁ DENTRO DE LA CARPETA templates/crud/ | |
<form id="crud_form" role="form"> | |
<div class="alert hide" id="ajax_crud"></div> | |
<div class="form-group"> | |
<label class="cole">Ejemplo:</label> | |
<?= Bootstrap::basic_input('text','ejemplo') ?> | |
</div> | |
<div class="form-group"> | |
<button type="button" id="crud" class="btn btn-primary">Enviar</button> | |
</div> | |
</form> | |
El javascript creado por el generador: | |
<script src="views/app/js/crud/crear.js"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* CRUD Model - Ocrend Framework | |
* Ejemplo de uso correcto para un CRUD usando rutas y la api rest | |
*/ | |
final class Crud extends Models implements OCREND { | |
public function __construct() { | |
parent::__construct(); | |
} | |
final public function errores(array $data) { | |
try { | |
if(Func::emp($data['ejemplo'])) { | |
throw new Exception('Error campo vacío.'); | |
} | |
return false; | |
} catch(Exception $e) { | |
return array('success' => 0, 'message' => $e->getMessage()); | |
} | |
} | |
final public function crear(array $data) : array { | |
$error = $this->errores($data); | |
if(false !== $error) { | |
return $error; | |
} | |
$i = array( | |
'ejemplo' => $data['ejemplo'] | |
); | |
# ó también: $i['ejemplo'] = $data['ejemplo']; | |
$this->db->insert('tabla',$i); | |
return array('success' => 1, 'message' => 'Éxito'); | |
} | |
final public function leer(bool $multi = true) { | |
if($multi) { | |
return $this->db->select('*','tabla'); | |
} | |
return $this->db->select('*','tabla',"id='$this->id'"); | |
} | |
final public function actualizar(array $data) : array { | |
$error = $this->errores($data); | |
if(false !== $error) { | |
return $error; | |
} | |
$i = array( | |
'ejemplo' => $data['ejemplo'] | |
); | |
# ó también: $i['ejemplo'] = $data['ejemplo']; | |
$this->id = $this->db->scape($data['id']); # porque desde la api rest no se define $this->id ya que no existe la ruta. | |
$this->db->update('tabla',$i,"id='$this->id'",'LIMIT 1'); | |
return array('success' => 1, 'message' => 'Éxito'); | |
} | |
final public function borrar() { | |
$this->db->delete('tabla',"id='$this->id'"); | |
Func::redir(URL . 'crud/'); | |
} | |
public function __destruct() { | |
parent::__destruct(); | |
} | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ASUMIMOS QUE ESTÁ DENTRO DE LA CARPETA templates/crud/ | |
<table> | |
<?php foreach(false !== $data ? $data : array() as $d): ?> | |
<tr> | |
<td><?= $d['ejemplo'] $?></td> | |
<td><a href="crud/editar/<?= $d['id'] ?>">Editar</a></td> | |
<td><a href="crud/eliminar/<?= $d['id'] ?>">Eliminar</a></td> | |
</tr> | |
<?php endforeach ?> | |
</table> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class crudController extends Controllers { | |
public function __construct() { | |
parent::__construct(); | |
Helper::load('bootstrap'); | |
$c = new Crud; | |
switch($this->method) { | |
case 'crear': | |
#crud/crear | |
echo $this->template->render('crud/crear'); | |
break; | |
case 'editar': | |
#crud/editar/1 | |
if($this->isset_id and false !== ($item = $c->leer(false))) { | |
echo $this->template->render('crud/editar', array( | |
'data' => $item[0] | |
)); | |
} else { | |
Func::redir(URL . 'crud/'); | |
} | |
break; | |
case 'eliminar': | |
#crud/eliminar/1 | |
$c->borrar(); | |
break; | |
default: | |
#crud/ | |
echo $this->template->render('crud/crud',array( | |
'data' => $c->leer() | |
)); | |
break; | |
} | |
} | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ASUMIMOS QUE ESTÁ DENTRO DE LA CARPETA templates/crud/ | |
<form id="crud_form" role="form"> | |
<div class="alert hide" id="ajax_crud"></div> | |
<input type="hidden" name="id" value="<?= $data['id'] ?>" /> | |
<div class="form-group"> | |
<label class="cole">Ejemplo:</label> | |
<?= Bootstrap::basic_input('text','ejemplo',false,$data['ejemplo']) ?> | |
</div> | |
<div class="form-group"> | |
<button type="button" id="crud" class="btn btn-primary">Enviar</button> | |
</div> | |
</form> | |
El javascript creado por el generador: | |
<script src="views/app/js/crud/editar.js"></script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment