Aula 04 - Modelo padrão (Read)




Depois de já ter criado a conexão com o banco, chegou a hora de começar a criar o CRUD, para isso vamos usar o arquivo chamado ASWmodel.php, nele colocaremos todo nosso CRUD e os outros models que estenderem ele, poderão usar todos esses métodos.

Imodel.php

namespace Acme\Interfaces;

interface Imodel{
    public function create($attributes);
    public function read();
    public function update($id,$attributes);
    public function delete($name,$value);
    public function findBy($name,$value);
}

ASWModel.php

namespace Asw\Database;

use Acme\Interfaces\Imodel;
use Asw\Database\Connection;
use PDOException;

class AswModel implements Imodel{

    private $database;
    private $attributes;

    public function __construct(){
        $database = new Connection;
        $this->database = $database->connection();
    }

    public function create($attributes){
    }

    public function read(){
        $query = "select * from $this->table";
        $pdo = $this->database->prepare($query);
        try{
            $pdo->execute();
            return $pdo->fetchAll();
        }catch(PDOException $e){
            dump($e->getMessage());
        }
    }

    public function update($id,$attributes){
    }

    public function delete($name,$value){
    }

    public function findBy($name,$value){
    }
}

*home.php

<h2 style="color: green;"><i class="user icon"></i>Cadastrar User</h2>

<form class="ui form" method="post">

<div class="filed">
    <label>User</label>
    <input type="text" name="name" placeholder="Digite um nome">
    <input type="hidden" name="cadastrar">
</div>

<div class="filed">
    <label>E-mail</label>
    <input type="text" name="email" placeholder="Digite um email">
<div class="filed">

<div class="filed">
    <label>Senha</label>
    <input type="text" name="password" placeholder="Digite uma senha">
<div class="filed">

<div style="margin-top: 10px;">
    <button class="ui blue button" type="submit"><i class="check green icon"></i>Cadastrar</button>
</div>
</form>

<div class="ui divider"></div>
<h2 style="color: green;"><i class="users icon"></i>Lista de users cadastrados</h2>

<table width="100%" class="ui table">
    <thead>
        <tr>
            <th>User</th>
            <th>Email</th>
            <th>Editar</th>
            <th>Deletar</th>
        </tr>
    </thead>
    <tbody>
        <?php
        $user = new Acme\Models\UserModel;
        $users = $user->read();
            foreach($users as $user):
        ?>
        <tr>
            <td><?php echo $user->name; ?></td>
            <td><?php echo $user->email; ?></td>
            <td><button class="ui green button"><i class="edit icon"></i>Editar</button></td>
             <td><button class="ui red button"><i class="remove icon"></i>Deletar</button></td>
        </tr>
        <?php endforeach; ?>
    </tbody>
</table>