modification sur le vocabulaire et sa gateway

php
Patrick BRUGIERE 2 years ago committed by patrick.brugiere
parent 4026ea5245
commit 9f92881e3f

@ -6,20 +6,27 @@ class Vocabulary
{ {
protected String $name; protected String $name;
protected String $image; protected String $image;
protected int $id;
protected int $aut;
/** /**
* @param String $name * @param String $name
* @param String $image * @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->name = $name;
$this->image = $image; $this->image = $image;
$this->id = $id;
$this->aut = $aut;
} }
public function __toString(): string 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; return $this->image;
} }
public function getId(): int
{
return $this->id;
}
public function getAut(): int
{
return $this->aut;
}
/** /**
* @param String $nom * @param String $nom
@ -75,6 +94,17 @@ class Vocabulary
$this->image = $image; $this->image = $image;
} }
public function setId(int $id): void
{
$this->id = $id;
}
public function setAut(int $aut): void
{
$this->aut = $aut;
}
} }
/* /*

@ -1,9 +1,8 @@
<?php <?php
namespace model; namespace model;
use model\Vocabulary;
use config\Connection; use config\Connection;
require_once('../config/Connection.php');
require_once('Vocabulary.php');
use PDO; use PDO;
class VocabularyGateway class VocabularyGateway
{ {
@ -23,7 +22,7 @@ class VocabularyGateway
$res = $this->con->getResults(); $res = $this->con->getResults();
$tab_vocab=[]; $tab_vocab=[];
foreach($res as $r){ 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; 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 "<br> avant <br>";
$g->addVocab(3,"gogo","img",2);
$g->addVocab(4,"gogo","img",2);
echo"apres <br>";
print_r($g->findByName('gogo'));
echo" <br> suppression normalement <br>";
$g->delVocabById(3);
print_r($g->findByName('gogo'));
echo "<br> modifié normalement <br>";
$g->ModifVocabById(4,"changer","new_img",4);
print_r($g->findByName('gogo'));
print_r($g->findByName('changer'));
Loading…
Cancel
Save