From 3bd3477d3bfd630f79e1b67396fda3a012f20dd8 Mon Sep 17 00:00:00 2001 From: parallels Date: Thu, 16 May 2024 18:06:15 +1000 Subject: [PATCH] Update keywords statistics at catalog time --- application/libraries/Catalog_item.php | 3 +++ application/models/Project_keyword_model.php | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/application/libraries/Catalog_item.php b/application/libraries/Catalog_item.php index 7139a334..0e8b8533 100644 --- a/application/libraries/Catalog_item.php +++ b/application/libraries/Catalog_item.php @@ -211,6 +211,9 @@ private function _process_keywords($project_id, $keywords_tag) $this->ci->project_keyword_model->insert(array('project_id'=>$project_id)); } } + // For keywords used in this project, update 'instances' field of 'keywords' table now, + // so correct keyword instances stats can be shown on catalog page even before keywords cron stats job runs. + $this->ci->project_keyword_model->set_keywords_statistics_by_project($project_id); } diff --git a/application/models/Project_keyword_model.php b/application/models/Project_keyword_model.php index 742afc7f..7ef98b56 100644 --- a/application/models/Project_keyword_model.php +++ b/application/models/Project_keyword_model.php @@ -2,6 +2,25 @@ class Project_keyword_model extends MY_Model { + public function set_keywords_statistics_by_project($project_id) + { + $sql = ' + UPDATE keywords k + JOIN + (SELECT pk.keyword_id as keyword_id, COUNT(pk.project_id) AS count + FROM project_keywords pk + GROUP BY 1 + ) as sub + ON k.id = sub.keyword_id + SET k.instances = sub.count + WHERE k.id IN + (SELECT keyword_id + FROM project_keywords + WHERE project_id = ?)'; + + + $query = $this->db->query($sql, array($project_id)); + } }