Ir para conteúdo
Fórum Script Brasil
  • 0

CRUD em DAO


leandrogs

Pergunta

E aí pessoal, como vai? Bom, estou desenvolvendo um pequeno sistema aqui onde eu posso fazer as operações Create, Read, Update e Delete (CRUD) utilizando os padrões DAO e MVC em PHP. Não posso fazer uso de PDO e o sistema não precisa ser flexível pois é apenas para exercitar os conceitos de DAO e MVC.

Agora segue o meu problema... Não consigo pensar em uma maneira para listar os registros no banco de dados! Estou dependendo desta parte para seguir em frente, ainda tenho muita coisa a ser feita. Seguem os códigos.

../controllers/MusicController.php

<?php

ini_set("display_errors", 1);
error_reporting(E_ALL);

require_once ("../models/Music.php");

class musicController {

    private $cadastre;
    private $update;
    private $delete;
    private $selection;

    public function __construct() {

        $action = $_POST['action'];
        if ($action == "insert") {
            $this->cadastre = new Music();
            $this->insert();
        } elseif ($action == "update") {
            $this->update = new Music();
            $this->update();
        } elseif ($action == "delete") {
            $this->delete = new Music();
            $this->delete();
        } elseif ($action == "select") {
            $this->selection = new Music();
            $this->select();
        }
    }

    private function insert() {
        $this->cadastre->setAlb_id($_POST['alb_id']);
        $this->cadastre->setArtist($_POST['artist']);
        $this->cadastre->setTitle($_POST['title']);
        $this->cadastre->setTrack($_POST['track']);

        $this->cadastre->insert();
    }

    private function update() {
        $this->update->setId($_POST['id']);
        $this->update->setAlb_id($_POST['alb_id']);
        $this->update->setArtist($_POST['artist']);
        $this->update->setTitle($_POST['title']);
        $this->update->setTrack($_POST['track']);

        $this->update->update();
    }

    private function delete() {
        $this->delete->setId($_POST['id']);

        $this->delete->delete();
    }

    private function select() {
        $this->selection->select();
    }

}

new musicController();
?>
../models/Music.php
<?php

require_once ("DataBase.php");

class music {

    private $id;
    private $track;
    private $alb_id;
    private $title;
    private $artist;
    private $dataBase;
    private $search;

    function __construct() {
        $this->dataBase = new dataBase();
    }

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

    public function setTrack($int) {
        $this->track = $int;
    }

    public function setAlb_id($int) {
        $this->alb_id = $int;
    }

    public function setTitle($string) {
        $this->title = $string;
    }

    public function setArtist($string) {
        $this->artist = $string;
    }

    public function setSearch($string) {
        $this->search = $string;
    }

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

    public function getTrack() {
        return $this->track;
    }

    public function getAlb_id() {
        return $this->alb_id;
    }

    public function getTitle() {
        return $this->title;
    }

    public function getArtist() {
        return $this->artist;
    }

    public function getSearch() {
        return $this->search;
    }

    public function insert() {
        $track = $this->getTrack();
        $alb_id = $this->getAlb_id();
        $title = $this->getTitle();
        $artist = $this->getArtist();

        $sql = "INSERT INTO music (track, alb_id, title, artist) VALUES 
            ('$track', '$alb_id', '$title', '$artist')";
        mysql_query($sql) or die(mysql_error());
    }

    public function update() {
        $id = $this->getId();
        $track = $this->getTrack();
        $alb_id = $this->getAlb_id();
        $title = $this->getTitle();
        $artist = $this->getArtist();

        $sql = "UPDATE music SET track='$track', alb_id='$alb_id', title='$title', 
        artist='$artist' WHERE id='$id'";

        mysql_query($sql);
    }

    public function delete() {
        $id = $this->getId();

        $sql = "DELETE FROM music WHERE id='$id'";

        mysql_query($sql);
    }

    public function select() {
        $sql = "SELECT * FROM music";

        $query = mysql_query($sql);
    }

}

?>
../models/DataBase.php
<?php

class dataBase {

    private $host;
    private $user;
    private $pass;
    private $dBase;

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

    private function connection() {
        $this->host = 'localhost';
        $this->user = 'root';
        $this->pass = '12345';
        $this->dBase = 'collection';

        mysql_connect($this->host, $this->user, $this->pass) or die(mysql_error());
        mysql_select_db($this->dBase);
    }

}

?>

O que está faltando é fazer um arquivo na view que liste as músicas cadastradas em meu banco de dados MySql.

Link para o comentário
Compartilhar em outros sites

1 resposta a esta questão

Posts Recomendados

Participe da discussão

Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.

Visitante
Responder esta pergunta...

×   Você colou conteúdo com formatação.   Remover formatação

  Apenas 75 emoticons são permitidos.

×   Seu link foi incorporado automaticamente.   Exibir como um link em vez disso

×   Seu conteúdo anterior foi restaurado.   Limpar Editor

×   Você não pode colar imagens diretamente. Carregar ou inserir imagens do URL.



  • Estatísticas dos Fóruns

    • Tópicos
      152,2k
    • Posts
      652k
×
×
  • Criar Novo...