Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class atencion {
- public static function get($peticion){
- if ($peticion[0] == 'getAll') {
- return self::getAll();
- }
- public static function getAll(){
- $sql = "SELECT * FROM persona";
- $sentencia = ConexionBD::obtenerInstancia()->obtenerBD()->prepare($sql);
- try{
- if ($sentencia->execute()) {
- $resultado = $sentencia->fetchAll(PDO::FETCH_CLASS);
- return $resultado;
- }
- else {
- return null;
- }
- }catch(PDOException $e){
- throw new ExcepcionApi(self::ESTADO_FALLA_DESCONOCIDA, "Falla desconocida". $e->getMessage(), 400);
- }
- }
- public static function post($peticion){
- if ($peticion[0] == 'agregar') {
- return self::agregar();
- } else {
- throw new ExcepcionApi(self::ESTADO_URL_INCORRECTA, "Url mal formada", 400);
- }
- }
- public static function agregar(){
- $body = file_get_contents('php://input');
- $input = json_decode($body);
- $array = (array)$input;
- $idPersona = $array['idPersona'];
- $documento = $array['documento'];
- $sql = "INSERT INTO persona (idPersona, documento) VALUES(:idPersona, :documento)";
- try{
- $pdo = ConexionBD::obtenerInstancia()->obtenerBD();
- $stmt = $pdo->prepare($sql);
- $stmt->bindParam(':idPersona', $idPersona);
- $stmt->bindParam(':documento', $documento);
- $stmt->execute();
- $pdo = null;
- echo '{"notice": {"text": "Persona Agregada"}';
- } catch(PDOException $e){
- echo '{"error": {"text": '.$e->getMessage(). '}';
- }
- }
- }
- $array=array("idPersona"=>1, "documento"=>7);
- $array["strDocumento"]=(string)$array["documento"];
- array(3) {
- ["idPersona"]=>
- int(1)
- ["documento"]=>
- int(7)
- ["idDocumento"]=>
- string(1) "7"
- }
- $array=array(
- array("idPersona"=>1, "documento"=>7),
- array("idPersona"=>2, "documento"=>-9),
- );
- foreach ($array as $k=>$row){
- $array[$k]["strDocumento"] = (string)$row["documento"];
- }
- var_dump($array);
- array(2) {
- [0]=>
- array(3) {
- ["idPersona"]=>
- int(1)
- ["documento"]=>
- int(7)
- ["strDocumento"]=>
- string(1) "7"
- }
- [1]=>
- array(3) {
- ["idPersona"]=>
- int(2)
- ["documento"]=>
- int(-9)
- ["strDocumento"]=>
- string(2) "-9"
- }
- }
Add Comment
Please, Sign In to add comment