<?php

declare(strict_types = 1);

namespace MyApp\Model;

use MyApp\Entity\Role;
use PDO;

class Role_Model{
    private PDO $db;

    public function __construct(PDO $db){
        $this->db = $db;
    }

    // on récup toutes les enregistrements de la BDD
    public function getAllRole():array{
        $sql = "SELECT * FROM Role";
        $stmt = $this->db->query($sql); // contient l'ensemble des résultats
        $role=[];
        // on organise sous forme d'un tableau les valeurs récupérées
        while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
            $role[] = new Role($row['Id_role'], $row['Nom_role']);
        }

        return $role;
    }

    public function getOneRole(int $id):?Role{
        $sql = "SELECT * from Role where Id_role = :id";
        $stmt = $this->db->prepare($sql);
        $stmt->bindValue(":id", $id);
        $stmt->execute();
        $row = $stmt->fetch(PDO::FETCH_ASSOC);
        if(!$row){
            return null;
        }
        return new Role($row['Id_role'], $row['Nom_role']);
    }


}
