You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
SAE_2A_FA-Reseau_ALICA/php/src/gateway/ImageGateway.php

100 lines
2.7 KiB

<?php
namespace App\gateway;
use App\metier\Image;
use PDO;
class ImageGateway
{
private Connection $con;
/**
* @param $con
*/
public function __construct(Connection $con)
{
$this->con = $con;
}
public function insertImage(Image $img)
{
$query = "INSERT INTO Image (`nom`, `taille`, `type`, `desc`, `blob`) VALUES ( :n, :t, :ty, :d, :b)";
$this->con->executeQuery($query, array(
':n' => array($img->getName(), PDO::PARAM_STR),
':t' => array($img->getTaille(), PDO::PARAM_STR),
':ty' => array($img->getType(), PDO::PARAM_STR),
':d' => array("desc", PDO::PARAM_STR),
':b' => array($img->getBlob(), PDO::PARAM_STR)
));
}
public function insert(int $id,string $name, string $desc, string $taille, string $type, string $blob)
{
$query = 'INSERT INTO Image VALUES (:i, :n, :t, :ty, :d, :b)';
$this->con->executeQuery($query, array(
':i' => array($id,PDO::PARAM_INT),
':n' => array($name, PDO::PARAM_STR),
':t' => array($taille, PDO::PARAM_STR),
':ty' => array($type, PDO::PARAM_STR),
':d' => array($desc, PDO::PARAM_STR),
':b' => array($blob, PDO::PARAM_STR)
));
}
public function delete(int $id)
{
$query = 'DELETE FROM Image WHERE id=:i';
$this->con->executeQuery($query, array(
':i' => array($id, PDO::PARAM_INT)
));
}
public function findById(int $id)
{
$query = 'SELECT * FROM Image WHERE id=:i';
$this->con->executeQuery($query, array(
':i' => array($id, PDO::PARAM_INT)
));
$res = $this->con->getResults();
return new Image($res[0]['nom'], $res[0]['desc'], $res[0]['taille'], $res[0]['type'], $res[0]['blob']);
}
public function findByNom(string $nom)
{
$query = 'SELECT * FROM Image WHERE nom=:n';
$this->con->executeQuery($query, array(
':n' => array($nom, PDO::PARAM_STR)
));
$res = $this->con->getResults();
return new Image($res[0]['nom'], $res[0]['taille'], $res[0]['type'], $res[0]['blob']);
}
public function getNewId()
{
$query = 'SELECT MAX(id) FROM Image';
$this->con->executeQuery($query);
$res = $this->con->getResults();
return $res[0]['MAX(id)'] + 1;
}
public function getAll()
{
$query = 'SELECT * FROM Image';
$this->con->executeQuery($query);
$res = $this->con->getResults();
$array = [];
foreach ($res as $r) {
$array[] = new Image($r['nom'], $r['taille'], $r['type'], $r['blob']);
}
return $array;
}
}
?>