I've been working on a PHP project for about 6 month where I'm using a multitier architecture (Data Access Layer , Business Logic Layer, ...).
One of the page of my website has a really long loading time, so I investigated and I realised it was the function from a BLL file that took a longer time to process.
Here's a example of a function in my DAL (here called DAL_Releve
) files :
public static function getReleveByAffectationID($id){
$conn = MgtConnexion::getConnexion();
$sql = "SELECT *"
. "FROM releve "
. "WHERE Affectation_ID = :id "
. "AND Date_Releve >= :date ;";
$req = $conn->prepare($sql);
$req->bindValue(":id", $id);
$req->bindValue(":date", $_SESSION["year"]-1 ."-01-01");
$req->execute();
MgtConnexion::closeConnexion($conn);
return $req;
}
Here's a example of a function in my BLL (here called MgtReleve
) files :
public static function getReleveByAffectationID($id){
$req = DAL_Releve::getReleveByAffectationID($id);
$releveArray = array();
while($row = $req->fetch(PDO::FETCH_ASSOC)){
$releveArray[] = new releve($row);
}
return $releveArray;
}
As you can see, I return the PDOStatement object $req
from the DAL and then fetch it in the BLL.
My problem is here : In my interface file, I'm looping through a array of Affectation
and each of them have Releves
(so each Releves
have an attribute Affectation_ID
), like this:
$lesAff = MgtAffectation::getAllAffectation();
//This doesn't return an array of objects but just an Array of arrays like : $array[<number>]["key"] = value;
foreach ($lesAff as $affect){
$lesReleves = MgtReleve::getReleveByAffectationID($affect["Affectation_ID"]);
foreach ($lesReleves as $rel){
//DO STUFF HERE
}
}
FYI : The page loads in 6.622 seconds with the code above and in 0.014 seconds if I remove everything inside the first foreach
(except for html code which is not showed here).
Now I'm starting to question my understanding of the multitiers architecture :
If I decide not to fetch $req
in my BLL file but in my interface (to loop only once in my queries results instead of twice), what's the purpose of the BLL then if only to return the PDOStatement object? Is there another way to return the data with more efficiency ?