ModelsUpdate.php 1.74 KB
<?php
class ModelsUpdate extends ModelsConn {
    private $tabela;
    private $dados;
    private $termos;
    private $values;
    private $result;
    private $msg;
    private $query;
    private $conn;

    public function exeUpdate($tabela, array $dados, $termos, $parseString) {
        $this->tabela = (string) $tabela;
        $this->dados = $dados;
        $this->termos = (string) $termos;

        parse_str($parseString, $this->values);
        $this->getIntrucao();
        $this->executarInstrucao();
    }

    public function getResult() {
        return $this->result;
    }

    public function getMsg() {
        return $this->msg;
    }

    public function getRowCount() {
        return $this->query->rowCount;
    }

    public function conexao() {
        $this->conn = parent::getConn();
        $this->query = $this->conn->prepare($this->query);
    }

    private function getIntrucao() {
        foreach ($this->dados as $key => $value) {
            $values[] = $key . ' = :' . $key;
        }
        $values = implode(', ', $values);
        $this->query = "UPDATE {$this->tabela} SET {$values} {$this->termos}";
    }

    private function executarInstrucao(){
        $this->conexao();
        try {
            $this->query->execute(array_merge($this->dados, $this->values));
            $this->result = true;
            $this->msg = "<div class=\"alert alert-danger\" role=\"alert\">
                              Usuário alterado com sucesso!
                          </div>";
        } catch (Exception $e) {
            $this->result = null;
            $this->msg = "<div class=\"alert alert-success\" role=\"alert\">
                              Erro ao Alterar</b> {$e->getMessage()}
                          </div>";
        }
    }
}