Advertisement
Guest User

Untitled

a guest
May 24th, 2019
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Model;
  4.  
  5. Class Conexao{
  6. // Usar singleton - manter somente uma instância da conexão
  7. private static $instance;
  8.  
  9. public static function getConn(){
  10. if(!isset($instance)){
  11. self::$instance = new \PDO('mysql:host=localhost;dbname:pdo;charset=utf8','root','root');
  12. return self::$instance;
  13. }
  14. }
  15. }
  16.  
  17. <?php
  18.  
  19. namespace App\Model;
  20.  
  21. Class Produto{
  22. private $id;
  23. private $nome;
  24. private $descricao;
  25.  
  26. public function getId(){
  27. return $this->id;
  28. }
  29.  
  30. public function setId($i){
  31. $this->id = $i;
  32. }
  33.  
  34. public function getNome(){
  35. return $this->nome;
  36. }
  37.  
  38. public function setNome($n){
  39. $this->nome = $n;
  40. }
  41.  
  42. public function getDescricao(){
  43. return $this->descricao;
  44. }
  45.  
  46. public function setDescricao($d){
  47. $this->descricao = $d;
  48. }
  49.  
  50. }
  51.  
  52. <?php
  53.  
  54. namespace App\Model;
  55.  
  56. Class ProdutoDao{
  57. public function create(Produto $p){
  58. $sql = 'INSERT INTO produtos (nome, descricao) VALUES (?, ?)';
  59. $stmt = Conexao::getConn()->prepare($sql);
  60. $stmt->bindValue(1, $p->getNome());
  61. $stmt->bindValue(2, $p->getDescricao());
  62. $stmt->execute();
  63. }
  64.  
  65. public function read(){
  66.  
  67. }
  68.  
  69. public function update(Produto $p){
  70.  
  71. }
  72.  
  73. public function delete($id){
  74.  
  75. }
  76.  
  77. }
  78.  
  79. index.php
  80.  
  81. <?php
  82.  
  83. require_once 'vendor/autoload.php';
  84.  
  85. $produto = new \App\Model\Produto();
  86. $produto->setNome('Notebook HP');
  87. $produto->setDescricao('i5, 4GB');
  88.  
  89. $prodDao = new \App\Model\ProdutoDao();
  90. $prodDao->create($produto);
  91.  
  92. Banco pdo
  93. create table produtos(
  94. id int primary key auto_increment,
  95. nome varchar(100) not null,
  96. descricao text
  97. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement