Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Model;
- Class Conexao{
- // Usar singleton - manter somente uma instância da conexão
- private static $instance;
- public static function getConn(){
- if(!isset($instance)){
- self::$instance = new \PDO('mysql:host=localhost;dbname:pdo;charset=utf8','root','root');
- return self::$instance;
- }
- }
- }
- <?php
- namespace App\Model;
- Class Produto{
- private $id;
- private $nome;
- private $descricao;
- public function getId(){
- return $this->id;
- }
- public function setId($i){
- $this->id = $i;
- }
- public function getNome(){
- return $this->nome;
- }
- public function setNome($n){
- $this->nome = $n;
- }
- public function getDescricao(){
- return $this->descricao;
- }
- public function setDescricao($d){
- $this->descricao = $d;
- }
- }
- <?php
- namespace App\Model;
- Class ProdutoDao{
- public function create(Produto $p){
- $sql = 'INSERT INTO produtos (nome, descricao) VALUES (?, ?)';
- $stmt = Conexao::getConn()->prepare($sql);
- $stmt->bindValue(1, $p->getNome());
- $stmt->bindValue(2, $p->getDescricao());
- $stmt->execute();
- }
- public function read(){
- }
- public function update(Produto $p){
- }
- public function delete($id){
- }
- }
- index.php
- <?php
- require_once 'vendor/autoload.php';
- $produto = new \App\Model\Produto();
- $produto->setNome('Notebook HP');
- $produto->setDescricao('i5, 4GB');
- $prodDao = new \App\Model\ProdutoDao();
- $prodDao->create($produto);
- Banco pdo
- create table produtos(
- id int primary key auto_increment,
- nome varchar(100) not null,
- descricao text
- );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement