From 4914b167fac9d54ae815c3bbee3a2c62160f4920 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 | 22 +++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) 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 67fafc94..a542e2f8 100644 --- a/application/models/Project_keyword_model.php +++ b/application/models/Project_keyword_model.php @@ -2,7 +2,27 @@ 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.count = sub.count + WHERE k.id IN + (SELECT keyword_id + FROM project_keywords + WHERE project_id = ?)'; + + + $query = $this->db->query($sql, array($project_id)); + } + + } /* End of file project_keyword.php */