<?php
/**
* Created by PhpStorm.
* User: Adeline
* Date: 30/01/2019
* Time: 19:29
*/
class Personne {
protected $id;
protected $nom;
protected $prenom;
protected $adresse1;
protected $adresse2;
protected $codePostal;
protected $email;
protected $ville;
protected $telephone;
protected $pays;
protected $handicap;
protected $permis;
protected $vehicule;
public function __construct( array $data =[] ) {
if (!empty($data)) {
$this->hydrate($data);
} else {
throw new Exception("Aucune donnees");
}
}
public function getId() {
return $this->id;
}
public function getNom() {
return $this->nom;
}
/**
* @return mixed
*/
public function getPrenom() {
return $this->prenom;
}
/**
* @return mixed
*/
public function getAdresse1() {
return $this->adresse1;
}
/**
* @return mixed
*/
public function getAdresse2() {
return $this->adresse2;
}
/**
* @return mixed
*/
public function getCodePostal() {
return $this->codePostal;
}
/**
* @return mixed
*/
public function getVille() {
return $this->ville;
}
/**
* @return mixed
*/
public function getPays() {
return $this->pays;
}
/**
* @return mixed
*/
public function gethandicap() {
return $this->handicap;
}
/**
* @return mixed
*/
public function getEmail() {
return $this->email;
}
/**
* @return mixed
*/
public function getTelephone() {
return $this->telephone;
}
/**
* @return mixed
*/
public function getPermis() {
return $this->permis;
}
/**
* @return mixed
*/
public function getVehicule() {
return $this->vehicule;
}
/**
* @param mixed $id_personne
*/
public function setId( $id ){
echo '<br>[debug]Dans "'.__FUNCTION__.'" [/debug]';
$id = (int) $id;
if ($id > 0)
{
$this->id = $id;
}
}
public function setNom( $nom ) {
if (strlen(trim($nom)) > 0)
//strlen = Calcule la taille d'une cha�ne
// trim = Supprime les espaces (ou d'autres caract�res) en d�but et fin de cha�ne
{
if (strpos($nom,"#") !== false)
// strpos = Cherche la position de la premi�re occurrence dans une cha�ne
{
throw new Exception("Le nom ne peut pas avoir de caracteres speciaux");
}
if (preg_match("/[0-9]/", "$nom"))
// preg_match = sert ici pour les cfiffres
{
throw new Exception("Le nom ne peut pas avoir de chiffre");
}
else
{
//echo "la cha�ne $nom est correcte";
$this->nom = $nom;
}
}
}
public function setPrenom( $prenom ) {
if (strlen(trim($prenom)) > 0)
{
if (strpos($prenom,"#") !== false)
{
throw new Exception("Le prenom ne peut pas avoir de caracteres speciaux");
}
if (preg_match("/[0-9]/", "$prenom"))
{
throw new Exception("Le prenom ne peut pas avoir de chiffre");
}
else
{
//echo "la cha�ne $prenom est correcte";
$this->prenom = $prenom;
}
}
}
/**
* @param mixed $adresse1
*/
public function setAdresse1( $adresse1 ) {
if (strlen(trim($adresse1)) > 0)
{
$this->adresse1 = $adresse1;
}else{
echo "l'adresse est obligatoire";
}
}
/**
* @param mixed $adresse2
*/
public function setAdresse2( $adresse2 ) {
if (strlen(trim($adresse2)) > 0)
{
$this->adresse2 = $adresse2;
}
}//pas de else
/**
* @param mixed $codePostal
*/
public function setCodePostal( $codePostal ) {
if (preg_match('/[0-9]{5}/',$codePostal))
{
$this->codePostal = $codePostal;
} else {
echo "<br/>Aucun résultat n'a été trouvé.";
}
}
public function setVille( $ville ) {
if (strlen(trim($ville)) > 0)
{
$this->ville = $ville;
if (preg_match("/[0-9]/", "$ville"))
{
throw new Exception("La ville ne peut pas avoir de chiffre");
}
}else{
echo "la ville est obligatoire";
}
}
/**
* @param mixed $pays
*/
public function setPays( $pays ) {
if (strlen(trim($pays)) > 0)
{
$this->pays = $pays;
if (preg_match("/[0-9]/", "$pays"))
{
throw new Exception("La ville ne peut pas avoir de chiffre");
}
}else{
echo "le pays est obligatoire";
}
}
/**
* @param mixed $email
*/
public function setEmail( $email ) {
//1) si la chaine n'est pas vide
if (strlen(trim($email)) == 0) {
//erreur
throw new LengthException("Le mail est vide",100); //code 100 == mail vide
} else {
//pas d'erreur on continue
// if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
// //mail valid�
$this->email = $email;
// } else {
// //erreur � g�rer
// throw new Exception("Le mail est invalide",101); //code 101 == mail invalide
// }
}
}
/**
* @param mixed $telephone
*/
public function setTelephone( $telephone ) {
$this->telephone = $telephone;
}
/**
* @param mixed $handicap
*/
public function setHandicap( $handicap ) {
$this->handicap = $handicap;
}
/**
* @param mixed $permis
*/
public function setPermis( $permis ) {
$this->permis = $permis;
}
/**
* @param mixed $vehicule
*/
public function setVehicule( $vehicule ) {
$this->vehicule = $vehicule;
}
protected function hydrate($array){
echo '<br>[debug]Dans "'.__FUNCTION__.'" [/debug]';
foreach ($array as $key => $value) {
$methodName = 'set'.ucfirst($key);
if(method_exists($this, $methodName)){
$this->$methodName($value);
}
}
}
}
<?php
/**
* Created by PhpStorm.
* User: Adeline
* Date: 11/11/2018
* Time: 17:54
*/
class Volontaire extends Personne {
protected $id;
protected $pre_inscrit;
public function __construct( array $array =[] )
{
$this->hydrate($array);
}
public function getId() {
return $this->id;
}
/**
* @return mixed
*/
public function getPre_inscrit()
{
return $this->pre_inscrit;
}
/**
* @param mixed $pre_inscrit
*/
public function setPre_inscrit($pre_inscrit)
{
$this->pre_inscrit = $pre_inscrit;
}
public function setId( $id ){
echo '<br>[debug]Dans "'.__FUNCTION__.'" [/debug]';
$id = (int) $id;
if ($id > 0)
{
$this->id = $id;
} else {
throw new Exception("Un identifiant est un entier");
}
}
protected function hydrate($array){
echo '<br>[debug]Dans "'.__FUNCTION__.'" [/debug]';
foreach ($array as $key => $value) {
$methodName = 'set'.ucfirst($key);
if(method_exists($this, $methodName)){
$this->$methodName($value);
}
}
}
}
<?php
/**
* Created by PhpStorm.
* User: Adeline
* Date: 11/11/2018
* Time: 17:56
*/
class ManagerVolontaire extends Manager {
public function __construct($mode='prod'){
parent::__construct($mode);
}
public function read($id){
echo '<br>[debug]Dans "'.__FUNCTION__.'" [/debug]';
$req = $this->db->prepare('SELECT * FROM volontaire WHERE $id=:$id');
$req->bindValue('$id', $id, PDO::PARAM_INT);
$req->execute();
$array = $req->fetch();
$volontaire = new volontaire($array);
return $volontaire;
}
public function add( $data ) {
echo '<br>[debug]Dans "'.__FUNCTION__.'" [/debug]';
//bloc try/catch pour gérer les exceptions
//provenant de Client
try {
$volontaire = new volontaire($data);
} catch (LengthException $lengthException) {
//cas longueur == 0
throw new Exception($lengthException->GetMessage(),$lengthException->GetCode());
} catch (Exception $exception) {
//autre cas (mais pour nous invalide)
throw new Exception($exception->GetMessage(),$exception->GetCode());
}
$req = $this->db->prepare('INSERT INTO volontaire (pre_inscrit,nom,prenom,adresse1,adresse2,codePostal,ville,pays,email,telephone,handicap,permis,vehicule) VALUES(:pre_inscrit,:nom,:prenom,:adresse1,:adresse2,:codePostal,:ville,:pays,:email,:telephone,:handicap,:permis,:vehicule)');
$req->bindValue('pre_inscrit', $volontaire->getPre_inscrit(), PDO::PARAM_STR);
$req->bindValue('nom', $volontaire->getNom(), PDO::PARAM_STR);
$req->bindValue('prenom', $volontaire->getPrenom(), PDO::PARAM_STR);
$req->bindValue('adresse1', $volontaire->getAdresse1(), PDO::PARAM_STR);
$req->bindValue('adresse2', $volontaire->getAdresse2(), PDO::PARAM_STR);
$req->bindValue('codePostal', $volontaire->getCodePostal(), PDO::PARAM_STR);
$req->bindValue('ville', $volontaire->getVille(), PDO::PARAM_STR);
$req->bindValue('pays', $volontaire->getPays(), PDO::PARAM_STR);
$req->bindValue('email', $volontaire->getEmail(), PDO::PARAM_STR);
$req->bindValue('telephone', $volontaire->getTelephone(), PDO::PARAM_STR);
$req->bindValue('handicap', ((is_null($volontaire->getHandicap())) ? ' ' : $volontaire->getHandicap()), PDO::PARAM_STR);
$req->bindValue('permis', $volontaire->getPermis(), PDO::PARAM_STR);
$req->bindValue('vehicule', $volontaire->getVehicule(), PDO::PARAM_STR);
$req->execute();
$id = $this->db->lastInsertId();
$volontaire->setId( $id );
}
public function getAllVolontaire() {
$stm = "SELECT * FROM volontaire";
//Ceci:
// $stmt = $this->connexion->query($req);
// return $stmt;
//resum� par cela
return $this->db->query( $stm );
}
public function getVolontaire( $id ) {
$stm = "SELECT * FROM volontaire WHERE id=:id";
//preparation == protection des donn?es ? venir
$stmt = $this->db->prepare( $stm );
//liaison des marqueur :toto aux donnees
$stmt->bindValue( 'id', $id, PDO::PARAM_INT );
//execution de la requete sur le serveur SQL
$stmt->execute();
return $stmt;
}
public function update( $data ) {
echo '<pre>' . print_r( $data, true ) . '</pre>';
echo '<br>[debug]SESSION';
echo '<pre>' . print_r( $data, true ) . '</pre>';
$req = $this->db->prepare('UPDATE volontaire SET nom=:nom,prenom=:prenom,adresse1=:adresse1,adresse2=:adresse2,codePostal=:codePostal,ville=:ville,pays=:pays,email=:email, telephone=:telephone, handicap=:handicap, permis=:permis, vehicule=:vehicule,pre_inscrit=:pre_inscrit WHERE id = :id');
$req->bindValue( 'id', $data->getId(), PDO::PARAM_INT );
$req->bindValue('nom', $data->getNom(), PDO::PARAM_STR);
$req->bindValue('prenom', $data->getPrenom(), PDO::PARAM_STR);
$req->bindValue('adresse1', $data->getAdresse1(), PDO::PARAM_STR);
$req->bindValue('adresse2', $data->getAdresse2(), PDO::PARAM_STR);
$req->bindValue('codePostal', $data->getCodePostal(), PDO::PARAM_STR);
$req->bindValue('ville', $data->getVille(), PDO::PARAM_STR);
$req->bindValue('pays', $data->getPays(), PDO::PARAM_STR);
$req->bindValue('email', $data->getEmail(), PDO::PARAM_STR);
$req->bindValue('telephone', $data->getTelephone(), PDO::PARAM_STR);
$req->bindValue('handicap', $data->getHandicap(), PDO::PARAM_STR);
$req->bindValue('permis', $data->getPermis(), PDO::PARAM_STR);
$req->bindValue('vehicule', $data->getVehicule(), PDO::PARAM_STR);
$req->bindValue('pre_inscrit', $data->getPre_inscrit(), PDO::PARAM_STR);
if (! $req->execute()) {
echo "<br>[debug] Erreur";
}
}
public function getListe() {
echo '<br>[debug]Dans "'.__FUNCTION__.'" [/debug]';
$req = $this->db->query('SELECT * FROM volontaire'); // 1 � N lignes
//init tableau des volontaires
$aVolontaires = array();
//Parcours des N lignes trouv�es dans la table Volontaire
while ($dataVolontaire = $req->fetch()) {
//on stocke un volontaire par case du tableau
$aVolontaires[] = new Volontaire($dataVolontaire);
}
//on retourne le tableau
return $aVolontaires;
}
public function delete( $id ) {
// =>voir getLivre(id) pour modele
$req = "DELETE FROM volontaire WHERE id=:id";
$stmt = $this->db->prepare( $req );
$stmt->execute();
if ( $stmt->rowCount() == 1 ) {
echo '[debug]OK 1 ligne inseree';
} else {
echo 'Erreur insertion donnees';
}
}
}
-- phpMyAdmin SQL Dump
-- version 4.8.0.1
-- https://www.phpmyadmin.net/
--
-- Hôte : 127.0.0.1:3306
-- Généré le : Dim 19 mai 2019 à 19:35
-- Version du serveur : 5.7.21
-- Version de PHP : 7.2.4
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- Base de données : `uniscite`
--
-- --------------------------------------------------------
--
-- Structure de la table `assurance`
--
DROP TABLE IF EXISTS `assurance`;
CREATE TABLE IF NOT EXISTS `assurance` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`nom` varchar(50) NOT NULL,
`codePostal` varchar(5) NOT NULL,
`email` varchar(500) NOT NULL,
`ville` varchar(500) NOT NULL,
`telephone` varchar(500) NOT NULL,
`pays` varchar(500) NOT NULL,
`numAssurer` varchar(50) NOT NULL,
`adresse1` varchar(500) NOT NULL,
`adresse2` varchar(500) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Structure de la table `assurance_habitation`
--
DROP TABLE IF EXISTS `assurance_habitation`;
CREATE TABLE IF NOT EXISTS `assurance_habitation` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`assurance` int(11) DEFAULT '3',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Structure de la table `assurance_vehicule`
--
DROP TABLE IF EXISTS `assurance_vehicule`;
CREATE TABLE IF NOT EXISTS `assurance_vehicule` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`assurance` int(11) DEFAULT '1',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Structure de la table `assurance_vie`
--
DROP TABLE IF EXISTS `assurance_vie`;
CREATE TABLE IF NOT EXISTS `assurance_vie` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`assurance` int(11) DEFAULT '2',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Structure de la table `assure`
--
DROP TABLE IF EXISTS `assure`;
CREATE TABLE IF NOT EXISTS `assure` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`assurance` int(11) DEFAULT '0',
`employe` int(11) DEFAULT '0',
`volontaire` int(11) DEFAULT '0',
PRIMARY KEY (`id`),
KEY `assurance` (`assurance`),
KEY `employe` (`employe`),
KEY `volontaire` (`volontaire`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Structure de la table `employe`
--
DROP TABLE IF EXISTS `employe`;
CREATE TABLE IF NOT EXISTS `employe` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`password` varchar(500) NOT NULL,
`nom` varchar(100) NOT NULL,
`prenom` varchar(100) NOT NULL,
`adresse1` varchar(500) NOT NULL,
`adresse2` varchar(500) NOT NULL,
`codePostal` varchar(5) NOT NULL,
`email` varchar(500) NOT NULL,
`ville` varchar(500) NOT NULL,
`telephone` varchar(15) NOT NULL,
`pays` varchar(15) NOT NULL,
`handicap` varchar(150) NOT NULL,
`permis` varchar(150) NOT NULL,
`vehicule` varchar(3) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
--
-- Déchargement des données de la table `employe`
--
INSERT INTO `employe` (`id`, `password`, `nom`, `prenom`, `adresse1`, `adresse2`, `codePostal`, `email`, `ville`, `telephone`, `pays`, `handicap`, `permis`, `vehicule`) VALUES
(1, '$2y$10$6juUOz1aLq6RyUyoXoROB.R13LjARAMQdbZwGDWVrpYr.cb4xt1eG', 'ddd', 'dffds', '4 tuope', 'rme', '65402', 'toto@gmail.com', 'pau', '0875652135', 'gre', 'oui', 'b', 'rte');
-- --------------------------------------------------------
--
-- Structure de la table `possede`
--
DROP TABLE IF EXISTS `possede`;
CREATE TABLE IF NOT EXISTS `possede` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`vehicule` int(11) DEFAULT '0',
`employe` int(11) DEFAULT '0',
`volontaire` int(11) DEFAULT '0',
PRIMARY KEY (`id`),
KEY `employe` (`employe`),
KEY `volontaire` (`volontaire`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Structure de la table `reunion`
--
DROP TABLE IF EXISTS `reunion`;
CREATE TABLE IF NOT EXISTS `reunion` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`date` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Structure de la table `reunion_employe`
--
DROP TABLE IF EXISTS `reunion_employe`;
CREATE TABLE IF NOT EXISTS `reunion_employe` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`reunion` int(11) DEFAULT '0',
`employe` int(11) DEFAULT '0',
`volontaire` int(11) DEFAULT '0',
PRIMARY KEY (`id`),
KEY `reunion` (`reunion`),
KEY `employe` (`employe`),
KEY `volontaire` (`volontaire`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Structure de la table `vehicule`
--
DROP TABLE IF EXISTS `vehicule`;
CREATE TABLE IF NOT EXISTS `vehicule` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`nom` varchar(500) NOT NULL,
`type_vehicule` varchar(500) NOT NULL,
`immatriculation` varchar(500) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
--
-- Déchargement des données de la table `vehicule`
--
INSERT INTO `vehicule` (`id`, `nom`, `type_vehicule`, `immatriculation`) VALUES
(1, 'kjh', 'pou', '2162');
-- --------------------------------------------------------
--
-- Structure de la table `volontaire`
--
DROP TABLE IF EXISTS `volontaire`;
CREATE TABLE IF NOT EXISTS `volontaire` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`pre_inscrit` varchar(50) DEFAULT '1',
`nom` varchar(100) NOT NULL,
`prenom` varchar(100) NOT NULL,
`adresse1` varchar(500) NOT NULL,
`adresse2` varchar(500) NOT NULL,
`codePostal` varchar(5) NOT NULL,
`email` varchar(500) NOT NULL,
`ville` varchar(500) NOT NULL,
`telephone` varchar(15) NOT NULL,
`pays` varchar(15) NOT NULL,
`handicap` varchar(150) NOT NULL,
`permis` varchar(150) NOT NULL,
`vehicule` varchar(3) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Contraintes pour les tables déchargées
--
--
-- Contraintes pour la table `assure`
--
ALTER TABLE `assure`
ADD CONSTRAINT `assure_ibfk_1` FOREIGN KEY (`assurance`) REFERENCES `assurance` (`id`),
ADD CONSTRAINT `assure_ibfk_2` FOREIGN KEY (`employe`) REFERENCES `employe` (`id`),
ADD CONSTRAINT `assure_ibfk_3` FOREIGN KEY (`volontaire`) REFERENCES `volontaire` (`id`);
--
-- Contraintes pour la table `possede`
--
ALTER TABLE `possede`
ADD CONSTRAINT `possede_ibfk_1` FOREIGN KEY (`employe`) REFERENCES `employe` (`id`),
ADD CONSTRAINT `possede_ibfk_2` FOREIGN KEY (`volontaire`) REFERENCES `volontaire` (`id`);
--
-- Contraintes pour la table `reunion_employe`
--
ALTER TABLE `reunion_employe`
ADD CONSTRAINT `reunion_employe_ibfk_1` FOREIGN KEY (`reunion`) REFERENCES `reunion` (`id`),
ADD CONSTRAINT `reunion_employe_ibfk_2` FOREIGN KEY (`employe`) REFERENCES `employe` (`id`),
ADD CONSTRAINT `reunion_employe_ibfk_3` FOREIGN KEY (`volontaire`) REFERENCES `volontaire` (`id`);
COMMIT;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
( ! ) Fatal error: Uncaught Error: Call to a member function prepare() on null in C:\wamp\www\uniscite\code\php\class\ManagerVolontaire.class.php on line 42
( ! ) Error: Call to a member function prepare() on null in C:\wamp\www\uniscite\code\php\class\ManagerVolontaire.class.php on line 42
× Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
× Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
Merci à tous. Vous pouvez me retrouver ici :
🌐 Visitez mon profil sur Comeup
🚀 Découvrez BeFreelancr
📱 Retrouvez-moi sur LinkedIn
N'oubliez pas d'activer les erreurs PDO.
Keep It Simple Stupid - SF4 conf Swift - Cours 1/4 SF4 - Exceptions PDO - Formes Normales