What would be the`Standard``in this case?

Asked

Viewed 85 times

0

How would the Padrão MVC in that case?

Well, I have a class Tipos, a class TiposDao and a formulário php to register a type.

That Class Tipos would be the Model. That Class TiposDao would be the Controller.

But how would the View of that Class?

It would be the form itself?

Or would it be the php file that lists the available types?

Follows the classes:

Tipo Class:

<?php
 class Tipos{

     private $idTipos;
     private $nome;

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

     public function setidTipos($_idTipos) {
         $this->idTipos = $_idTipos;
     }

     public function getidTipos () {
         return $this->idTipos;
     }

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

}
?>

Class Tiposdao:

<?php 
 class TiposDao {

     private $conexao;

     public function __construct ($_conexao) {      
         $this->conexao = $_conexao;
     }

     public function excluir ($idTipo) {         

         $string = "DELETE FROM tipos WHERE idTipos = ".$idTipo;
         $this->conexao->query($string);

     }

     public function cadastrar ($tipo) {         

         $string = "INSERT INTO tipos (nome) VALUES ('".$tipo->getNome()."')";
         $this->conexao->query($string);

     }

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

     public function editar ($tipo) {        

         $string = "UPDATE tipos 
                    SET 
                      nome = '".$tipo->getNome()."'
                    WHERE 
                      idTipos = ".$tipo->getidTipos();
        $this->conexao->query($string);
     }


     public function pesquisaTipoNome($nome) {
         $tipo = null;           

         $string = "SELECT idTipos, nome
                    FROM tipos 
                    WHERE nome= '".$nome."'";

         $registros = $this->conexao->query($string);
         $quantasLinhas = $registros->num_rows;      

         if ($quantasLinhas > 0)
         {
             list ($idTipos, $nome) = $registros->fetch_row();
             $tipo = new tipos($nome);                       
             $tipo->setidTipos($idTipos);
         }

         return $tipo;

     }

     public function pesquisaTipoId($idTipo) {
         $tipo = null;           

         $string = "SELECT idTipos, nome 
                    FROM tipos 
                    WHERE idTipos = ".$idTipo;

         $registros = $this->conexao->query($string);
         $quantasLinhas = $registros->num_rows;      

         if ($quantasLinhas > 0)
         {

             list ($idTipos, $nome) = $registros->fetch_row();
             $tipo = new Tipos($nome);                       
             $tipo->setidTipos($idTipos);
         }

         return $tipo;

     }

     public function pesquisaNomeTipo($idTipo) {
         $tipo = null;           

         $string = "SELECT nome 
                    FROM tipos 
                    WHERE idTipos = ".$idTipo;

         $registros = $this->conexao->query($string);
         $quantasLinhas = $registros->num_rows;      

         if ($quantasLinhas > 0)
         {

             list ($nome) = $registros->fetch_row();
             $tipo = $nome;
         }

         return $tipo;

     }

     public function pesquisaTipos() {
         $tipos = null;           

         $string = "SELECT idTipos, nome FROM tipos";

         $registros = $this->conexao->query($string);
         $quantasLinhas = $registros->num_rows; 

         if ($quantasLinhas > 0) {

             while (list ($idTipos, $nome) = $registros->fetch_row()) {

                 $tipo = new Tipos($nome);                       
                 $tipo->setidTipos($idTipos);

                 $tipos[] = $tipo;
             }
         }

         return $tipos;

     }
 }
?>

Registered fileTipos.php

<?php

 require_once "../_controlls/_daos/AdminsDao.php";
 require_once "../_controlls/_models/Tipos.php";
 require_once "../_controlls/_daos/TiposDao.php";



$AdminsDao = new AdminsDao($conexao);
$TiposDao = new TiposDao($conexao);

if(isset($_GET["acao"]) && $_GET["acao"] == "form") {
?>
  <h1 class="h1Centralizado">Cadastrar Tipo</h1>
<div style="width:500px">  
  <form action="?" method="post">
    <input type="hidden" name="acao" value="cadastrar" /> <br />
    <label class="labelPequeno">Nome</label><input type="text" class="typeTextMedio" maxlength="200" id="nome" name="nome" required /> <br /> <br />
    <input type="submit" value="Cadastrar Tipo" /><br /> <br />
  </form>
</div>
<?php   
}

if (isset($_POST["acao"]) && $_POST["acao"] == "cadastrar") {

      $nome = $_POST["nome"];

    if( $nome == "" ) {
        echo "<h1 class='h1CentralizadoAvisos'>Algum campo ficou sem preecher!</h1>";     
        echo "<br />";
        echo "<div style='text-align:center'><a href='javascript:history.go(-1)'><img src='_img/voltar.png' title='Voltar' /></a><br /><br />";  
    }
    else
    {       
            $tipo = new Tipos($nome);                   
            $TiposDao->cadastrar($tipo);

                echo "<h1 class='h1Centralizado'>Cadastro feito com sucesso!</h1>";       
                echo "<br />";
                echo "<div style='text-align:center'><a href='javascript:history.go(-1)'><img src='_img/voltar.png' title='Voltar' /></a><br /><br />";  

              $connection->fechaConexao();
    }   

}
?>
  • If "DAO types" follow the DAO standard, and serve to perform operations in the database, in the types table, I believe it is also part of the model. Controller would be the class that uses DAO type to gain access to "Type" objects. The view will only receive Controller parameters, and based on this, render the screen (HTML) that will be displayed to the user.

  • 1

    Is it really necessary to do this on the arm? I recommend using a framework, like Laravel or Cakephp, if possible.

  • Your view is doing more things than it should, more about this see here

  • My View would be the registered fileTipos;php?

  • 1

    Yes, I would take that file, but the idea of MVC is that the view only takes the values, then forwards it to the controler, which in turn passes the data to the model (or DAO) to persist in the database. I believe that creating an MVC structure, without even knowing the pattern, is a bad idea, try to use some framework, see how things work in it.

  • Just a hint: MVC and PHP do not work well together. The ideal framework for "forcing" the programmer to use MVC is JSF. I recommend you take a look as you are thinking of following an MVC architecture.

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.