How to insert data from a json in a mysql table with phalcon?

0

I am currently working with the php phalcon framework and I have a problem when saving information to the database with json,

make an ajax that loads me some inputs depending on the code of a team

<script type="text/javascript">
$("#codigoEquipo").keydown(function () {
    var codigo = $("#codigoEquipo").val();
    $.ajax({
     "url" : "{{url("circulacion/ajax")}}",
     "type" : "get",
     "dataType": "json",
     "data" : {
          "codigoEquipo" : codigo
     },
     beforeSend : function () {
           $("#loading_form").show();
     }
    }).done(function (response) {
           $("#loading_form").hide();
           $("#serial").val(response.serialActivo)
           $("#tipoActivo").val(response.tipoActivo)
           $("#numeroDocumento").val(response.numeroDocumento)
           $("#nombres").val(response.nombres)
           $("#apellidos").val(response.apellidos)
           $("#idCargo").val(response.idCargo)
    })
});

</script>

and in my controller the following:

public function ajaxAction()
{
    //deshabilitamos la vista para peticiones ajax
    $this->view->disable();
    //si es una petición get y es una petición ajax
    if($this->request->isGet() == true && $this->request->isAjax() == true)
    {
        //obtenemos el codigo del equipo para hacer el filtro
        $codigoEquipo = $this->request->get("codigoEquipo");

        //parámetros para hacer la clausula where
        $parameters = array(
            "codigoEquipo" => $codigoEquipo,
        );

        //obtenemos el código del equipo
        $usuariosActivos = usuariosactivos::findFirst(array(
            "conditions" => "codigoEquipo = :codigoEquipo:",
            "columns" => "serialActivo,tipoActivo,numeroDocumento,nombres,apellidos,idCargo",
            "bind" => $parameters
        ));

        $this->response->setJsonContent(array(
            "serialActivo" => str_pad($usuariosActivos->serialActivo, 1, '0', STR_PAD_LEFT),
            "tipoActivo" => str_pad($usuariosActivos->tipoActivo, 1, '0', STR_PAD_LEFT),
            "numeroDocumento" => str_pad($usuariosActivos->numeroDocumento, 1, '0', STR_PAD_LEFT),
            "nombres" => str_pad($usuariosActivos->nombres, 1, '0', STR_PAD_LEFT),
            "apellidos" => str_pad($usuariosActivos->apellidos, 1, '0', STR_PAD_LEFT),
            "idCargo" => str_pad($usuariosActivos->idCargo, 1, '0', STR_PAD_LEFT)
        ));
        $this->response->setStatusCode(200, "OK");
        $this->response->send();

    }
    else
    {
        $this->response->setStatusCode(404, "Not Found");
        $this->response->send();
    }
    return $this->response;
}

My problem is when inserting the data that the json brought me, that data is brought from a table that is called active users and they are shown in a form called circulation, I want to save it in that circulation table, like I do ??

    
asked by mari jaramillo 09.12.2016 в 18:59
source

2 answers

1

Hi I found two ways to insert data in BD with Phalcon and I hope it helps you:

<?php
 use Phalcon\Http\Request;
 class NombreControlador extends \Phalcon\Mvc\Controller{
     public $request;

    function initialize(){        
      $this->request = new Request();       
    }
    public function postAction(){         

     //Obtener los parametros por metodo POST
     $parametros = $this->request->getJsonRawBody(true);

     //Instancia del modelo a guardar 
     $modelo = new Modelo();

     //Primera forma se envían los parametros como se obtuvieron
     if(!$modelo->save($parametros)){
       //Mostrar el error
     }

     //segunda forma accediendo por cada propiedad del modelo
     $modelo->nombreColumna = $parametros->campo;
     if(!$modelo->save()){
      //mostrar error
     }
   }

 }
    
answered by 21.03.2018 в 16:21
0

You should use a personalized Behavior or create the methods afterFetch () and beforeSave () in your model to do the conversion

<?php 
class usuariosactivos {
    //cambiar columna a json antes de guardar
    public function beforeSave() {
        $this->jsonColumn = json_encode((array) $this->jsonColumn);
    }
    //volver a array la columna
    public function afterFetch() {
        $this->jsonColumn = json_decode((string) $this->jsonColumn);
    }
}
    
answered by 11.02.2017 в 01:19