Project Improvement in PHP

1

I have a question about design pattern in php. I have a class DAO that has a selection method, well what happens is that I had to put some stylized HTML codes with bootstrap in this method, what did I do this correct?

class CategoriaDao {
public function selecionaTudo($sql) {
    $conexao = new PDOUtil();
    $consulta = $conexao->conectar()->query($sql);     
    while ($sqlSeleciona = $consulta->fetch(PDO::FETCH_OBJ)){       
        echo "<td>" . $sqlSeleciona->descricao . "</td>";
        echo '<td><a href="index.php?action=update&id='.$sqlSeleciona->id_categoria_pagina.'" class="btn btn-default btn-sm"><span class="glyphicon glyphicon-edit"></span> Editar</a></td>';
        echo '<td><a href="index.php?action=delete&id='.$sqlSeleciona->id_categoria_pagina.'" class="btn btn-default btn-sm"><span class="glyphicon glyphicon-remove"></span> Excluir</a></td>';
        echo "</tr>";   
    }
}
    
asked by anonymous 30.03.2015 / 02:16

3 answers

1

The ideal is to remove the HTML from PHP logic, so the code can be reused without any side effects such as Can not modify header information another point is your class should have a single responsibility and its methods should only perform one task, selecionaTudo does two things manipulate the database and formats a text output (which is not the responsibility of the CategoryDao class) / p>

If there are few records displayed and the select has no where you can simplify the method this way:

public function selecionaTudo($sql) {
    $conexao = new PDOUtil();
    return $conexao->conectar()->query($sql)->fetchAll(PDO::FETCH_ASSOC));
}

Suggestions

Instead of creating a connection variable for each method in your class, you can make it a class member and call PDOUtil in the DAO constructor.

class CategoriaDao {
   private $conexao;

   public function __constructor($db){
     $this->conexao = $db;
   }

   public function selecionaTudo($sql) {
     return $this->conexao->query($sql)->fetchAll(PDO::FETCH_ASSOC);
   }
 }

The call would look like this:

$DAOCategoria = new CategoriaDao(PDOUtil::conectar());
$categoria = $DAOCategoria->selecionarTudo($sql);

As you are not using any frameworks, one way to separate the php html is to use a template engine like smarty .

    
30.03.2015 / 13:53
0

Separate the logic from the view.

Deliver the query object, such as an array of items, in the preview (HTML) do a simple while. So you are not required to only use Bootstrap and you can take your class in another project.

One tip is to have your Bootstrap class, so grab the object with the items and apply your call to this class, being specific to the Boostrap.

    
30.03.2015 / 03:26
0

Are you creating a pattern or do you want to follow a pattern? Before thinking about the code we have to think about the design architecture. Although you say you have no logic problem, this method does not echo the opening of "<tr>" , generating a potentially problematic table for browser engines that do not compensate for badly created tags (IE up to 8, for example). He would also meet the principle of generality since his research will need to return a "id_categoria_pagina" "guaranteed" and also would have problems with the encapsulation, since the opening of the table and its closure would be in other classes. If they are in the same entity (something like CategoriaView ) it might not be a problem, but would still seriously consider leaving what "view" is in the View. ;)

    
30.03.2015 / 03:54