CRUD with MVC and DAO in PHP

2

I'm starting with programming and I'm doing a CRUD with MVC and DAO. I would like to know if the form I'm doing is correct, what could be improved and how do I call the method of inserting the ProductController in the form.

<?php

    class Database {

        private $host = "localhost";
        private $username = "root";
        private $password = "123456";
        private $database = "crud";


        public function conecta() 
        {
            $conexao = new mysqli($this->host, $this->username, $this->password, $this->database);
            return $conexao;        
        }
    }
<?php 

    class Produto {

        private $id;
        private $nome;
        private $descricao;
        private $preco;

        public function getId() {
            return $this->id;
        }

        public function setId($id) {
            $this->id = $id;
        }

        public function getNome() {
            return $this->nome;
        }

        public function setNome($nome) {
            $this->nome = $nome;
        }

        public function getDescricao() {
            return $this->descricao;
        }

        public function setDescricao($descricao) {
            $this->descricao = $descricao;
        }

        public function getPreco() {
            return $this->preco;
        }

        public function setPreco($preco) {
            $this->preco = $preco;
        }

    }
<?php  

    class ProdutoDAO {

        function adiciona(Database $conexao, Produto $produto) {

            $query = "INSERT INTO produtos (nome, descricao, preco) VALUES ('{$produto->getNome()}', '{$produto->getDescricao()}', '{$produto->getPreco()}')";  
            mysqli_query($conexao->conecta(), $query);
        }

    }
<?php  

require_once ('../models/Produto.php');
require_once ('../models/ProdutoDAO.php');
require_once ('../config/Database.php');

class ProdutoController {

    public function insere() {
        $nome = $_POST['nome'];
        $descricao = $_POST['descricao'];
        $preco = $_POST['preco'];
        $conexao = new Database();
        $produto = new Produto();
        $produto->setNome($nome);
        $produto->setPreco($preco);
        $produto->setDescricao($descricao);
        $produtoDao = new ProdutoDao();
        $produtoDao->adiciona($conexao, $produto);
    }

}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="../../public/css/bootstrap.min.css" rel="stylesheet">
<title>Adiciona Produto</title>
</head>
<body>
<div class="container">
<h3>Adicionar Produto</h3>
<form method="post" action="../../controllers/ProdutoController.php">
  <div class="form-group">
    <label>Nome</label>
    <input type="text" class="form-control" name="nome">
  </div>
  <div class="form-group">
    <label>Descrição</label>
    <input type="text" class="form-control" name="descricao">
  </div>
  <div class="form-group">
    <label>Preço</label>
    <input type="text" class="form-control" name="preco">
  </div>
  <button type="submit" class="btn btn-primary">Adicionar</button>
</form>
</div>
</body>
</html>
    
asked by anonymous 14.11.2016 / 19:03

2 answers

2

I would change some details:

Database class

class Database 
{
    private $host = "localhost";
    private $username = "root";
    private $password = "123456";
    private $database = "crud";
    private $conexao = null; 

    public function __construct()
    {          
        $this->conect();
    }

    public function getConection()
    {
        return $this->conexao;
    }

    private function conect() 
    {
        $this->conexao = mysqli_connect(
                  $this->host, 
                  $this->username, 
                  $this->password, 
                  $this->database);
    }
}

class Product

class ProdutoDAO 
{
    private $db;
    public function __construct(Database $db)
    {
        $this->db = $db;
    }

    public function add(Produto $produto) 
    {
        $nome = $produto->getNome();
        $descricao = $produto->getDescricao();
        $preco = $produto->getPreco();

        $query = "INSERT INTO produtos (nome, descricao, preco) VALUES(?,?,?)"; 
        $stmt = mysqli_prepare($this->db->getConection(), $query);
        mysqli_stmt_bind_param($stmt,'sss', $nome, $descricao, $preco);
        mysqli_stmt_execute($stmt); 
        mysqli_stmt_close($stmt);
    }

}

In addition to these changes, I think the biggest question would be how to execute this all by calling for a request of a <form />

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="../../public/css/bootstrap.min.css" rel="stylesheet">
<title>Adiciona Produto</title>
</head>
<body>
<div class="container">
<h3>Adicionar Produto</h3>
<form method="post" action="../../controllers/ProdutoController.php">
  <div class="form-group">
    <label>Nome</label>
    <input type="text" class="form-control" name="nome">
  </div>
  <div class="form-group">
    <label>Descrição</label>
    <input type="text" class="form-control" name="descricao">
  </div>
  <div class="form-group">
    <label>Preço</label>
    <input type="text" class="form-control" name="preco">
  </div>
  <button type="submit" class="btn btn-primary">Adicionar</button>
</form>
</div>
</body>
</html>

As in your <form /> o action call ProdutoController.php , create a file with the same name and:

<?php  

require_once ('../models/Produto.php');
require_once ('../models/ProdutoDAO.php');
require_once ('../config/Database.php');

$db      = new Database();
$dao     = new ProdutoDao($db);

$produto = new Produto();
$produto->setNome($nome);
$produto->setPreco($preco);
$produto->setDescricao($descricao);

$dao->add($produto); // aqui grava o resultado enviado do form

redirect('Location:index.php');
    
14.11.2016 / 19:49
0
Come on!

To begin with, I would like to highlight the true concept of MVC:

MVC is a software architecture standard that separates information (and its business rules) from the interface that the user interacts with.

In other words, your code should not be defined in a giant block as if it were a structured programming (as in C, for example);

It's kind of tricky for someone to tell you how to "call" a method ... in programming, we must follow every step to learn by thinking in a big way!

I suggest you take a good look at the link I'll send to the end to learn all the step-by-step and become a good programmer in the future!

I hope to remedy your future doubts! ;)

link

    
14.11.2016 / 19:12