From d0ae2e1a4ffcc77741b392dd74940ffe12d0c1d3 Mon Sep 17 00:00:00 2001 From: divyesh000 Date: Sat, 8 Jun 2024 14:36:40 +0400 Subject: [PATCH 1/2] 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 +} From 018f2434fbd900c8f79aa134e87f8006821dd8c4 Mon Sep 17 00:00:00 2001 From: divyesh000 Date: Sun, 9 Jun 2024 11:19:06 +0400 Subject: [PATCH 2/2] modify SQL query in Product model to calculate rating percentage distribution --- src/models/Product.php | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/models/Product.php b/src/models/Product.php index c24527e..d33ed78 100644 --- a/src/models/Product.php +++ b/src/models/Product.php @@ -393,18 +393,18 @@ public function getRatingDistribution(): array { // Query the database to get the percentage distribution of ratings $query = <<< EOL - SELECT rating, - COUNT(*) * 100.0 / ( - SELECT COUNT(*) + SELECT rating, + COUNT(*) * 10.0 / ( + SELECT COUNT(*) + FROM order_product op + JOIN `order` o ON op.order_id = o.order_id + WHERE op.product_id = :product_id + ) AS percentage 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 + JOIN `order` o ON r.client_id = o.client_id + JOIN order_product op ON op.order_id = o.order_id + WHERE op.product_id = :product_id + GROUP BY rating; EOL; $params = ['product_id' => $this->product_id];