1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
<?php
require 'Connexion.php';
class ContactManager
{
public function __construct()
{
$this->bdd = (new Connexion())->getPDO();
}
private function create(Contact &$contact) {
$stmt = $this->bdd->prepare('INSERT INTO table_taille (taille, taille2, taille3, nom, prenom, ville, pays) VALUES (:taille, :taille2, :taille3, :nom, :prenom, :ville, :pays)');
$stmt->bindValue(':taille', $contact->getTaille(), PDO::PARAM_STR);
$stmt->bindValue(':taille2', $contact->getTaille2(), PDO::PARAM_STR);
$stmt->bindValue(':taille3', $contact->getTaille3(), PDO::PARAM_STR);
$stmt->bindValue(':nom', $contact->getNom(), PDO::PARAM_STR);
$stmt->bindValue(':prenom', $contact->getPrenom(), PDO::PARAM_STR);
$stmt->bindValue(':ville', $contact->getVille(), PDO::PARAM_STR);
$stmt->bindValue(':pays', $contact->getPays(), PDO::PARAM_STR);
$executeIsOK = $stmt->execute();
if(!$executeIsOK) {
echo 'ERREUR stmt CREATE : ';
var_dump($stmt->errorInfo());
return false;
} else {
$id = $this->bdd->lastInsertId();
$contact = $this->read($id);
return $id;
}
}
public function read($id) {
$stmt = $this->bdd->prepare('SELECT * FROM table_taille WHERE id = :id');
$stmt->bindValue(':id',$id, PDO::PARAM_INT);
$executeIsOK = $stmt->execute();
if($executeIsOK) {
$contact = $stmt->fetchObject('Contact');
if($contact === false){
return null;
} else {
return $contact;
}
} else {
return false;
}
}
private function update(Contact $contact){
$stmt = $this->bdd->prepare('UPDATE table_taille set taille=:taille, taille2=:taille2, taille3=:taille3 WHERE id=:id LIMIT 1');
$stmt->bindValue(':taille', $contact->getTaille(), PDO::PARAM_STR);
$stmt->bindValue(':taille2', $contact->getTaille2(), PDO::PARAM_STR);
$stmt->bindValue(':taille3', $contact->getTaille3(), PDO::PARAM_STR);
$stmt->bindValue(':nom', $contact->getNom(), PDO::PARAM_STR);
$stmt->bindValue(':prenom', $contact->getPrenom(), PDO::PARAM_STR);
$stmt->bindValue(':ville', $contact->getVille(), PDO::PARAM_STR);
$stmt->bindValue(':pays', $contact->getPays(), PDO::PARAM_STR);
$res = $stmt->execute();
if ($res){
return $contact->getId();
} else {
return false;
}
}
public function save(contact $contact){
if(is_null($contact->getId())){
return $this->create($contact);
} else {
return $this->update($contact);
}
}
} |
Partager