From 572debd0e820bfaa4d55b59eb19544fa245aa579 Mon Sep 17 00:00:00 2001 From: Dhruv Date: Sun, 1 Jun 2014 13:38:27 +0530 Subject: [PATCH] [ticket/11445] Optimize no of queries in get_global_subscriptions PHPBB3-11445 --- phpBB/phpbb/notification/manager.php | 29 ++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/phpBB/phpbb/notification/manager.php b/phpBB/phpbb/notification/manager.php index c3539e76df..8378af2d99 100644 --- a/phpBB/phpbb/notification/manager.php +++ b/phpBB/phpbb/notification/manager.php @@ -587,26 +587,34 @@ class manager $subscriptions = array(); + $sql = 'SELECT method, notify, item_type + FROM ' . $this->user_notifications_table . ' + WHERE user_id = ' . (int) $user_id . ' + AND item_id = 0'; + + $result = $this->db->sql_query($sql); + $rows = array(); + + while ($row = $this->db->sql_fetchrow($result)) + { + $rows[$row['item_type']][] = $row; + } + + $this->db->sql_freeresult($result); + foreach ($this->get_subscription_types() as $group_name => $types) { foreach ($types as $id => $type) { - $sql = 'SELECT method, notify - FROM ' . $this->user_notifications_table . ' - WHERE user_id = ' . (int) $user_id . " - AND item_type = '" . $this->db->sql_escape($id) . "' - AND item_id = 0"; - $result = $this->db->sql_query($sql); - $row = $this->db->sql_fetchrow($result); - if (!$row) + if (empty($rows[$id])) { // No rows at all, default to '' $subscriptions[$id] = array(''); } else { - do + foreach ($rows[$id] as $row) { if (!$row['notify']) { @@ -620,10 +628,7 @@ class manager $subscriptions[$id][] = $row['method']; } - while ($row = $this->db->sql_fetchrow($result)); } - - $this->db->sql_freeresult($result); } }