ModelsRead.php
2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
class ModelsRead extends ModelsConn {
private $select;
private $values;
private $result;
private $msg;
private $query;
private $conn;
public function exeRead($tabela, $termos = null, $parseString = null) {
if (!empty($parseString)) {
// Caso tenha sido passado algum parâmetro, ele será colocado no array $this->values
parse_str($parseString, $this->values);
}
// Montando a SQL para busca em BD
$this->select = "SELECT * FROM {$tabela} {$termos}";
$this->executarInstrucao();
}
private function getInstrucao(){
if ($this->values) {
foreach ($this->values as $link => $valor) {
if ($link == 'limit' || $link == 'offset') {
$valor = (int)$valor;
}
// garantido a integridade dos valores passados (bindvalue)
$this->query->bindValue(":{$link}", $valor, (is_int($valor) ? PDO::PARAM_INT : PDO::PARAM_STR));
}
}
}
private function executarInstrucao(){
$this->conexao();
try{
// associando os atributos aos identificadores para a SQL
$this->getInstrucao();
$this->query->execute();
// traz todas as linhas do registro
$this->result = $this->query->fetchAll();
}catch (PDOException $e){
$this->result = null;
$this->msg = "<strong>Erro ao Ler dados:</strong> {$e->getMessage()}";
}
}
private function conexao(){
// chamando o método da classe pai
$this->conn = parent::getConn();
// preparando a SQL
$this->query = $this->conn->prepare($this->select);
// colocando o modo de exibição da query como ASSOCIAÇÃO (nome dos campos)
$this->query->setFetchMode(PDO::FETCH_ASSOC);
}
// GETTERS IMPORTANTES PARA ACESSO EXTERNO
public function getResult(){
return $this->result;
}
public function getMsg(){
return $this->msg;
}
public function getRowCount() {
return $this->query->rowCount();
}
}