Les produits – php dao

–diag de classe–
–bd–
–acces modificateur–
–DAO–

Intro

  • réaliser le diagramme de classe de la base de données

Partie 1

Livrable gitlab : ristoo/mission2/…

  • réaliser la base de données
  • enregistrer au minimum 10 boissons, 10 desserts, 10 plats avec leur prix et 5 clients fidèles

Pour permettre la réalisation de l’application, nous allons référencer les images de chaque produit dans la base de données.

  • sauvegarder des images de chaque produit dans un répertoire de votre serveur web
  • ajouter un champ image dans chaque table
  • indiquer l’url de l’image du produit dans ce champ

Partie 2

Livrable gitlab : ristoo/mission2/…

  • réaliser les classes PHP boisson, dessert et plat dans Boisson.php, Dessert.php et Plat.php avec les accesseurs, modificateurs et une methode __toString
<?php

class Boisson {

public string $nom;
private string $prix;
private int $id;



public function __construct($nom, $prix,$id=0){
    $this->id = $id;
    $this->nom = $nom;
    $this->prix = $prix;
    }

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

public function setNom(string $nom) {
    $this->nom = $nom;
}

public function getNom():string {
    return $this->nom;
}

public function getPrix():string {
    return $this->prix;
}
public function setPrix(string $prix) {
    $this->prix=$prix;
}

public function __toString(){
  return $this->id." ".$this->nom." ".$this->prix;
}
}

 

  • réaliser les classes DAO correspondantes (voir cours PHP section Data Access Object) en implémentant l’interface managerInterface.php dans les fichiers BoissonDao.php, DessertDao.php et PlatDao.php
interface managerInterface {
     function setConnection() ;
       function select(int $id);
       function selectN(string $nom);
      function insert( $obj);
      function update( $obj);
      function delete( $obj): bool;
      function findAll(): array;
}
<?php include_once "ManagerInterface.php";
include_once "Boisson.php";

class BoissonDAO implements ManagerInterface {
    private PDO $connection;
    
    
    public function setConnection() {
        $dsn='mysql:dbname=ristoo;host=localhost:3306';
        $user='root';
        $password='root';
    try{
        $dbh=new PDO($dsn,$user,$password); 
        $this->connection=$dbh;
    }catch(PDOException $e){
        echo'Connexion échouée:'.$e->getMessage(); 
    }
        
    }
  
    public function insert($obj):bool {
        $result=false;
        try {
            $nom=$obj->getNom();
            $prix=$obj->getPrix();
          $sql="INSERT INTO boissons (nom_boissons, prix) VALUES ('$nom', '$prix')";
            echo $sql;
            $nbLignes=$this->connection->exec($sql) ;
            if($nbLignes==1)
              $result=true;
            
        } catch (PDOException $e) {
            echo'insert échoué:'.$e->getMessage(); 
        }
        return $result;
    }
    public function selectN(string $nom):Boisson {
        // $maBoisson = new Boisson();
         try {
             
             $sql="SELECT * FROM boissons where nom_boissons = '".$nom."' limit 1";
             echo $sql;
             $statement=$this->connection->query($sql,PDO::FETCH_ASSOC) ;
             $row  = $statement -> fetch();
             $maBoisson = new Boisson($row['nom_boissons'], $row['prix'],$row['id']);
         //    $b->setNom($row['nom_boissons']);
         //    $b->setId($row['id']);
         return $maBoisson;
         } catch (PDOException $e) {
             echo'select échoué:'.$e->getMessage(); 
         }
     }
    public function select(int $id):Boisson {
       // $maBoisson = new Boisson();
        try {
            $sql="SELECT * FROM boissons where id = '$id'";
            $statement=$this->connection->query($sql,PDO::FETCH_ASSOC) ;
            $row  = $statement -> fetch();
            $maBoisson = new Boisson($row['nom_boissons'], $row['prix'],$row['id']);
        //    $b->setNom($row['nom_boissons']);
        //    $b->setId($row['id']);
        return $maBoisson;
        } catch (PDOException $e) {
            echo'select échoué:'.$e->getMessage(); 
        }
    }
    public function update($obj): bool{
        $result=false;
        try {
            $id=$obj->getId();        
            $nom=$obj->getNom();
          $sql="UPDATE boissons SET nom_boissons = '$nom' WHERE id = '$id'";
            echo $sql;
            $nbLignes=$this->connection->exec($sql) ;
            if($nbLignes==1)
              $result=true;
            
        } catch (PDOException $e) {
            echo'update échoué:'.$e->getMessage(); 
        }
        return $result;
    }

    public function delete($obj): bool{

    }
  public function findAll():array { 
$personnes = array(); 
try { 
$sql="SELECT * FROM boisson"; 
$statement=$this->connection->query($sql); 
$personnes = $statement->fetchAll(PDO::FETCH_CLASS, "Boisson");
 } 
catch (PDOException $e) {
 echo'select all échoué:'.$e->getMessage();
 } 
return $personnes; }
}

echo "Début du programme";
$maBoisson=new Boisson("Pepsi", 5);   
$maBoisson->getNom();
$maBoisson->getPrix();
$boissondao=new BoissonDAO();
$boissondao->setConnection();
$boissondao->insert($maBoisson);
var_dump($boissondao);

$b=$boissondao->select(9);
var_dump($b);

$maBoisson=$boissondao->selectN("Pepsi");
var_dump($maBoisson);
$maBoisson->setNom("riri");
var_dump($maBoisson);
$boissondao->update($maBoisson);
//$maBoisson->setId();
//$maBoisson->setNom("Minute Maid");
//$maBoisson->setPrix("2");
//$boissondao->update($maBoisson);
//$maBoisson=$boissondao->select(1);
//var_dump($boissondao);
//echo "<br><br>".$boissondao."<br><br>";


 

  • afficher toutes les boissons sous forme de texte grâce à findAll de BoissonDao
  • réaliser un formulaire qui permette d’enregistrer un dessert et son image
  • enregistrer le dessert grâce à insert de DessertDao

 

Partie 3

Livrable gitlab : ristoo/mission2/…

  • réaliser une page web boissons.php qui affiche toutes les boissons grâce à BoissonDao.
    • Le nom apparaîtra
    • l’id sera dans un champ caché et concaténé avec la première lettre du type de produit, b1 pour boisson 1
  • réaliser une page web plats.php qui affiche tous les plats
  • réaliser une page web desserts.php qui affiche tous les desserts

 

dessert e1584108136882