Last active
May 9, 2017 22:50
-
-
Save YurePereira/e0962c3c53a5c97caee526cef8ab655e to your computer and use it in GitHub Desktop.
Código para CRUD PDO
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class Aluno { | |
| private $codigo; | |
| private $nome; | |
| private $email; | |
| private $telefone; | |
| private $celular; | |
| private $status; | |
| private $instituicao; | |
| private $curso; | |
| private $senha; | |
| private $dataRegistro; | |
| private $ip; | |
| private $periodo; | |
| private $ciclo; | |
| private $ra; | |
| private $rg; | |
| private $cpf; | |
| public function getCodigo(){ | |
| return $this->codigo; | |
| } | |
| public function setCodigo($codigo){ | |
| $this->codigo = $codigo; | |
| } | |
| public function getNome(){ | |
| return $this->nome; | |
| } | |
| public function setNome($nome){ | |
| $this->nome = $nome; | |
| } | |
| public function getEmail(){ | |
| return $this->email; | |
| } | |
| public function setEmail($email){ | |
| $this->email = $email; | |
| } | |
| public function getTelefone(){ | |
| return $this->telefone; | |
| } | |
| public function setTelefone($telefone){ | |
| $this->telefone = $telefone; | |
| } | |
| public function getCelular(){ | |
| return $this->celular; | |
| } | |
| public function setCelular($celular){ | |
| $this->celular = $celular; | |
| } | |
| public function getStatus(){ | |
| return $this->status; | |
| } | |
| public function setStatus($status){ | |
| $this->status = $status; | |
| } | |
| public function getInstituicao(){ | |
| return $this->instituicao; | |
| } | |
| public function setInstituicao($instituicao){ | |
| $this->instituicao = $instituicao; | |
| } | |
| public function getCurso(){ | |
| return $this->curso; | |
| } | |
| public function setCurso($curso){ | |
| $this->curso = $curso; | |
| } | |
| public function getSenha(){ | |
| return $this->senha; | |
| } | |
| public function setSenha($senha){ | |
| $this->senha = sha1($senha); | |
| } | |
| public function getDataRegistro(){ | |
| return $this->dataRegistro; | |
| } | |
| public function setDataRegistro($dataRegistro){ | |
| $this->dataRegistro = $dataRegistro; | |
| } | |
| public function getIp(){ | |
| return $this->ip; | |
| } | |
| public function setIp($ip){ | |
| $this->ip = $ip; | |
| } | |
| public function getPeriodo(){ | |
| return $this->periodo; | |
| } | |
| public function setPeriodo($periodo){ | |
| $this->periodo = $periodo; | |
| } | |
| public function getCiclo(){ | |
| return $this->ciclo; | |
| } | |
| public function setCiclo($ciclo){ | |
| $this->ciclo = $ciclo; | |
| } | |
| public function getRa(){ | |
| return $this->ra; | |
| } | |
| public function setRa($ra){ | |
| $this->ra = $ra; | |
| } | |
| public function getRg(){ | |
| return $this->rg; | |
| } | |
| public function setRg($rg){ | |
| $this->rg = $rg; | |
| } | |
| public function getCpf(){ | |
| return $this->cpf; | |
| } | |
| public function setCpf($cpf){ | |
| $this->cpf = $cpf; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class AlunoDAO extends DAO { | |
| public function __construct() { | |
| parent::__construct(); | |
| } | |
| public function insert(Aluno $aluno) { | |
| $this->db->aluno()->insert(array( | |
| 'nm_aluno' => $aluno->getNome(), | |
| 'ds_email' => $aluno->getEmail(), | |
| 'ds_telefone' => $aluno->getTelefone(), | |
| 'ds_celular' => $aluno->getCelular(), | |
| 'ds_status' => $aluno->getStatus(), | |
| 'ds_instituicao' => $aluno->getInstituicao(), | |
| 'ds_curso' => $aluno->getCurso(), | |
| 'ds_senha' => $aluno->getSenha(), | |
| 'ds_ip' => $aluno->getIp(), | |
| 'ds_periodo' => $aluno->getPeriodo(), | |
| 'ds_ciclo' => $aluno->getCiclo(), | |
| 'cd_ra' => $aluno->getRa(), | |
| 'cd_rg' => $aluno->getRg(), | |
| 'cd_cpf' => $aluno->getCpf() | |
| )); | |
| return true; | |
| } | |
| public function read($field = '*', $where = '1') { | |
| $alunos = array(); | |
| foreach ($this->db->aluno()->select($field)->where($where) as $value) { | |
| $aluno = new Aluno(); | |
| $aluno->setCodigo( isset( $value['cd_aluno'] ) ? $value['cd_aluno'] : '' ); | |
| $aluno->setNome( isset( $value['nm_aluno'] ) ? $value['nm_aluno'] : '' ); | |
| $aluno->setEmail( isset( $value['ds_email'] ) ? $value['ds_email'] : '' ); | |
| $aluno->setTelefone( isset( $value['ds_telefone'] ) ? $value['ds_telefone'] : '' ); | |
| $aluno->setCelular( isset( $value['ds_celular'] ) ? $value['ds_celular'] : '' ); | |
| $aluno->setStatus( isset( $value['ds_status'] ) ? $value['ds_status'] : '' ); | |
| $aluno->setInstituicao( isset( $value['ds_instituicao'] ) ? $value['ds_instituicao'] : '' ); | |
| $aluno->setCurso( isset( $value['ds_curso'] ) ? $value['ds_curso'] : '' ); | |
| $aluno->setSenha( isset( $value['ds_senha'] ) ? $value['ds_senha'] : '' ); | |
| $aluno->setIp( isset( $value['ds_ip'] ) ? $value['ds_ip'] : '' ); | |
| $aluno->setPeriodo( isset( $value['ds_periodo'] ) ? $value['ds_periodo'] : '' ); | |
| $aluno->setCiclo( isset( $value['ds_ciclo'] ) ? $value['ds_ciclo'] : '' ); | |
| $aluno->setRa( isset( $value['cd_ra'] ) ? $value['cd_ra'] : '' ); | |
| $aluno->setRg( isset( $value['cd_rg'] ) ? $value['cd_rg'] : '' ); | |
| $aluno->setCpf( isset( $value['cd_cpf'] ) ? $value['cd_cpf'] : '' ); | |
| $aluno->setDataRegistro( isset( $value['dt_registro'] ) ? $value['dt_registro'] : '' ); | |
| $alunos[] = $aluno; | |
| } | |
| return $alunos; | |
| } | |
| public function delete($code) { | |
| $deleted = $this->db->aluno()->where('cd_aluno = ' . $code); | |
| return $deleted && $deleted->delete(); | |
| } | |
| public function update($code, Array $data) { | |
| $uploaded = $this->db->aluno()->where('cd_aluno = ' . $code); | |
| return $uploaded && $uploaded->update($data); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class Connection extends PDO { | |
| private $host; | |
| private $dbms; | |
| private $dbname; | |
| private $username; | |
| private $password; | |
| private $option = array( | |
| PDO::ATTR_ERRMODE => true, | |
| PDO::ERRMODE_EXCEPTION => true | |
| ); | |
| private $handle; | |
| public function __construct($host, $dbms, $dbname, $username = '', $password = '') { | |
| $this->host = $host; | |
| $this->dbms = $dbms; | |
| $this->dbname = $dbname; | |
| $this->username = $username; | |
| $this->password = $password; | |
| try { | |
| $this->handle = $this->getDrive($this->dbms, $this->dbname, $this->host, $this->option, $this->username, $this->password); | |
| } catch (PDOException $e) { | |
| print 'Erro: ' . $e->getMessage(); | |
| } | |
| } | |
| private function getDrive($dbms, $dbname, $host, $option, $username = '', $password = '') { | |
| switch ($dbms) | |
| { | |
| case 'mysql': | |
| return parent::__construct("mysql:host=$host;dbname=$dbname;charset=latin1", $username, $password, $option); | |
| break; | |
| case 'pgsql': | |
| return parent::__construct("pgsql:dbname={$dbname};user={$username}; password={$password};host=$host"); | |
| break; | |
| case 'sqlite': | |
| return parent::__construct("sqlite:{$dbname}"); | |
| break; | |
| case 'ibase': | |
| return parent::__construct("firebird:dbname={$dbname}", $username, $password); | |
| break; | |
| case 'oci8': | |
| return parent::__construct("oci:dbname={$dbname}", $username, $password); | |
| break; | |
| case 'mssql': | |
| return parent::__construct("mssql:host={$host},1433;dbname={$dbname}", $username, $password); | |
| break; | |
| default: | |
| return null; | |
| } | |
| } | |
| public function __destruct() { | |
| $this->handle = null; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| abstract class DAO { | |
| protected $pdo = null; | |
| protected $db = null; | |
| public function __construct() { | |
| $this->pdo = new Connection('127.0.0.1', 'mysql', 'NOME_BANCO', 'root', ''); | |
| $this->db = new NotORM($this->pdo, new NotORM_Structure_Convention); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| $aluno = new Aluno(); | |
| $alunoDAO = new AlunoDAO(); | |
| $aluno->setNome('Test'); | |
| $aluno->setEmail('test@test.com'); | |
| $aluno->setTelefone($a['4444-4444']); | |
| $aluno->setCelular($a['4444-4444']); | |
| //... | |
| $result = $alunoDAO->insert($aluno); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment