From 9f92881e3f28b5de7d447809a1e4817b461d18a9 Mon Sep 17 00:00:00 2001 From: "patrick.brugiere" Date: Tue, 31 Oct 2023 18:53:31 +0100 Subject: [PATCH] modification sur le vocabulaire et sa gateway --- Project/php/model/Vocabulary.php | 34 +++++++++++- Project/php/model/VocabularyGateway.php | 72 +++++++++++++++++++++++-- 2 files changed, 99 insertions(+), 7 deletions(-) diff --git a/Project/php/model/Vocabulary.php b/Project/php/model/Vocabulary.php index a7c00bf..b8e3dc8 100755 --- a/Project/php/model/Vocabulary.php +++ b/Project/php/model/Vocabulary.php @@ -6,20 +6,27 @@ class Vocabulary { protected String $name; protected String $image; + protected int $id; + protected int $aut; /** * @param String $name * @param String $image + * @param int $id + * @param int $aut */ - public function __construct( $name, $image) + public function __construct( int $id,string $name, string $image, int $aut) { $this->name = $name; $this->image = $image; + $this->id = $id; + $this->aut = $aut; } + public function __toString(): string { - return "Vocabulaire :" . $this->name . $this->image; + return "Vocabulaire :" . $this->id . $this->name . $this->image . $this->aut; } /* @@ -58,6 +65,18 @@ class Vocabulary return $this->image; } + public function getId(): int + { + return $this->id; + } + + public function getAut(): int + { + return $this->aut; + } + + + /** * @param String $nom @@ -75,6 +94,17 @@ class Vocabulary $this->image = $image; } + public function setId(int $id): void + { + $this->id = $id; + } + + public function setAut(int $aut): void + { + $this->aut = $aut; + } + + } /* diff --git a/Project/php/model/VocabularyGateway.php b/Project/php/model/VocabularyGateway.php index f224de1..143cea9 100755 --- a/Project/php/model/VocabularyGateway.php +++ b/Project/php/model/VocabularyGateway.php @@ -1,9 +1,8 @@ array($name,PDO::PARAM_STR)); $this->con->ExecuteQuery($query,$args); $res = $this->con->getResults(); $tab_vocab=[]; foreach($res as $r){ - $tab_vocab[]=new Vocabulary($r['id'],$r['name'],$r['image']); + $tab_vocab[]=new Vocabulary($r['id'],$r['name'],$r['image'],$r['creator']); } Return $tab_vocab; } @@ -34,8 +33,71 @@ class VocabularyGateway } + public function addVocab(int $id, String $name, String $img, int $aut):void{ + try{ + $query = "INSERT INTO Vocabulary values(:id,:name,:img,:aut)"; + $args = array(':id'=>array($id,PDO::PARAM_INT), + ':name'=>array($name,PDO::PARAM_STR), + ':img'=>array($img,PDO::PARAM_STR), + ':aut'=>array($aut,PDO::PARAM_INT)); + $this->con->ExecuteQuery($query,$args); + } + catch (\PDOException $e){ + error_log('PDOException: ' . $e->getMessage(), 3, 'error.log'); + + } + + } + + public function delVocabById(int $id):void{ + try{ + $query = "DELETE FROM Vocabulary v WHERE v.id=:id "; + $args = array(':id'=>array($id,PDO::PARAM_INT)); + $this->con->ExecuteQuery($query,$args); + } + catch (\PDOException $e){ + error_log('PDOException: ' . $e->getMessage(), 3, 'error.log'); + + } + + } + + public function ModifVocabById(int $id, String $name,String $img,String $aut):void{ + try{ + $query = "UPDATE Vocabulary SET name=:name, image=:img, creator=:aut WHERE id=:id"; + $args = array(':id'=>array($id,PDO::PARAM_INT), + ':name'=>array($name,PDO::PARAM_STR), + ':img'=>array($img,PDO::PARAM_STR), + ':aut'=>array($aut,PDO::PARAM_INT)); + $this->con->ExecuteQuery($query,$args); + } + catch (\PDOException $e){ + error_log('PDOException: ' . $e->getMessage(), 3, 'error.log'); + + } + + } + } +$con = new Connection('mysql:host=localhost;dbname=project','root',''); +$g = new VocabularyGateway($con); +print_r($g->findByName('gogo')); +echo "
avant
"; + +$g->addVocab(3,"gogo","img",2); +$g->addVocab(4,"gogo","img",2); +echo"apres
"; +print_r($g->findByName('gogo')); + +echo"
suppression normalement
"; + +$g->delVocabById(3); +print_r($g->findByName('gogo')); +echo "
modifié normalement
"; +$g->ModifVocabById(4,"changer","new_img",4); +print_r($g->findByName('gogo')); +print_r($g->findByName('changer')); \ No newline at end of file