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.
– mau humor
Is it really necessary to do this on the arm? I recommend using a framework, like Laravel or Cakephp, if possible.
– mau humor
Your view is doing more things than it should, more about this see here
– rray
How and why to use MVC in PHP?
– rray
My View would be the registered fileTipos;php?
– Carlos Rocha
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.
– mau humor
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.
– Giuliana Bezerra