parent
4f52d41603
commit
7c57f0adcf
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
class Connection extends PDO {
|
||||
|
||||
private $stmt;
|
||||
|
||||
public function __construct(string $dsn, string $username, string $password) {
|
||||
parent::__construct($dsn,$username,$password);
|
||||
$this->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
}
|
||||
|
||||
|
||||
/** * @param string $query
|
||||
* @param array $parameters *
|
||||
* @return bool Returns `true` on success, `false` otherwise
|
||||
*/
|
||||
|
||||
public function executeQuery(string $query, array $parameters = []) : bool{
|
||||
$this->stmt = parent::prepare($query);
|
||||
foreach ($parameters as $name => $value) {
|
||||
$this->stmt->bindValue($name, $value[0], $value[1]);
|
||||
}
|
||||
|
||||
return $this->stmt->execute();
|
||||
}
|
||||
|
||||
public function getResults() : array {
|
||||
return $this->stmt->fetchall();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
include('Connection.php');
|
||||
include('user.php');
|
||||
|
||||
Class UserGateway{
|
||||
private Connection $con;
|
||||
|
||||
public function __construct(Connection $con){
|
||||
$this->con=$con;
|
||||
}
|
||||
|
||||
public function insert(USer $u):int{
|
||||
|
||||
// récupération id
|
||||
$query='SELECT id_user FROM Users WHERE id_user >= ALL (SELECT id_user FROM User);';
|
||||
$this->con->executeQuery($query);
|
||||
$result=$this->con->getResults();
|
||||
$id=$result['id_user'] + 1;
|
||||
|
||||
// insertion user
|
||||
$query='INSERT INTO Users VALUES (id=:id,username=:username,email=:email,passwd=:passwd,CURRENT_DATE);';
|
||||
$this->con->executeQuery($query,array(':id' => array($id,PDO::PARAM_INT),':username' => array($u->username,PDO::PARAM_STR),':email' => array($u->email,PDO::PARAM_STR),':passwd' => array($u->passwd,PDO::PARAM_STR)));
|
||||
return $this->con->lastInsertId();
|
||||
}
|
||||
}
|
||||
|
||||
$u = new User("Test","1234","none","test@gmail.com");
|
||||
$uG = new UserGateway(new Connection("mysql:host=localhost;dbname=dbkemondejar","",""));
|
||||
$uG->insert($u);
|
||||
?>
|
Loading…
Reference in new issue