Angled Factory to enter data into the bank

2

How do I create a function, in factory, to enter data in the database? I have this:

.factory('pegaContas', ['$http', function($http) {
    var _getContasEntrada = function(id_empresa) {
        return $http.post("php/index.php", id_empresa);
    };

    var _setContasEntrada = function(conta) {
    }

    return {
        getContasEntrada: _getContasEntrada
        setContasEntrada: _setContasEntrada
    }
}])

And my function / method in php of insertion data is in a class:

    <?php
    function insereContaEntrada($id_empresa, $cat, $subcat, $val, $forPag, $data){
        $pdo = conectar();
        $val = floatval(str_replace(',', '.', str_replace('.', '', $val)));

        if($data == ''){
            $data = date("Y-m-d");
        }

        $this->insereDadosEntrada=$pdo->prepare(
            "INSERT INTO entrada (id_entrada, id_empresa, categoria, subcategoria, valor, forma_pagamento, data) 
             VALUES (?, ?, ?, ?, ?, ?, ?)");
        $this->insereDadosEntrada->bindValue(1, NULL);
        $this->insereDadosEntrada->bindValue(2, $id_empresa);
        $this->insereDadosEntrada->bindValue(3, $cat);
        $this->insereDadosEntrada->bindValue(4, $subcat);
        $this->insereDadosEntrada->bindValue(5, $val);
        $this->insereDadosEntrada->bindValue(6, $forPag);
        $this->insereDadosEntrada->bindValue(7, $data);
        //$this->insereDadosEntrada->execute();
        try {
            $this->insereDadosEntrada->execute();
            echo "Cadastro efetuado com sucesso!";
        } catch (Exception $e) {
            print_r($this->insereDadosEntrada->errorInfo());
        }
    }
?>

I want to call this method in the angle.

    
asked by anonymous 21.11.2015 / 15:09

1 answer

2

Well, you first need to tell php which function you want to call. The method I use for this is through action , where my link in .factory would look like this:

var _getContasEntrada = function(id_empresa) {
   return $http.post("php/index.php?action=insereContaEntrada",id_empresa).then( 
        function(res) { 
            console.log(res); //Verifica o que o php está retornando
            return res.data;
        },
        function(err) {alert(feedbackError);}
    );
};

In php , I use switch to determine the action types that can be called, like this:

switch($_GET['action']) {
    case 'insereContaEntrada': insereContaEntrada();
    break;
}

function insereContaEntrada()....

The data process is done differently (at least for me). I look for the data coming through the function and I make the decode of it, because it will come in the JSON model, this step is at your discretion (I believe). But at the beginning of the function, use:

$result = json_decode(file_get_contents("php://input"));

In this way, you will convert the data coming into an array that you can work on better. Or, just use the file_get_contents("php://input") code to get the raw data.

Ps: I'm not very good in the PHP area, so I may be wrong about using only the latest code, but I think you now have an idea of how it works.

Edited:

Now that I've noticed, your code has some syntax errors, I do not know if it was just here or in your code, but it should look like this:

.factory('pegaContas', ['$http', function($http) {
    var _getContasEntrada = function(id_empresa) {
        return $http.post("php/index.php", id_empresa);
    };

    var _setContasEntrada = function(conta) {
    };

    return {
        getContasEntrada: _getContasEntrada,
        setContasEntrada: _setContasEntrada
    }
}])

In the return block, the name with the prefix _ is only for you to identify which is which. In this case, the name without prefix is the name you use inside a controller to call a function inside the factory. The prefixed name is to identify which function within the factory should be called. Only best practice to organize yourself better. And it should always have a comma separated except the last definition.

    
21.11.2015 / 15:24