From d0ae2e1a4ffcc77741b392dd74940ffe12d0c1d3 Mon Sep 17 00:00:00 2001 From: divyesh000 Date: Sat, 8 Jun 2024 14:36:40 +0400 Subject: [PATCH] update getRatingDistribution() : modify SQL query in rating percentage calculation --- src/models/Product.php | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/models/Product.php b/src/models/Product.php index 1c687bc..c24527e 100644 --- a/src/models/Product.php +++ b/src/models/Product.php @@ -85,7 +85,7 @@ public static function getCategories(): array return []; } - $callback = fn($obj): string => $obj->category; + $callback = fn ($obj): string => $obj->category; return array_map($callback, $result); } @@ -394,7 +394,14 @@ public function getRatingDistribution(): array // Query the database to get the percentage distribution of ratings $query = <<< EOL SELECT rating, - COUNT(*) * 100.0 / (SELECT COUNT(*) FROM review WHERE product_id = :product_id) AS percentage + COUNT(*) * 100.0 / ( + SELECT COUNT(*) + FROM review r + JOIN order_product op ON r.client_id = op.client_id + JOIN `order` o ON op.order_id = o.order_id + WHERE r.product_id = :product_id + AND op.product_id = :product_id + ) AS percentage FROM review WHERE product_id = :product_id GROUP BY rating @@ -433,4 +440,4 @@ public function updateProduct(array $newProductData): bool return $this->update($newProductData, ['product_id' => $this->product_id], $this->table); } -} \ No newline at end of file +}