[ticket/11445] Optimize no of queries in get_global_subscriptions

PHPBB3-11445
This commit is contained in:
Dhruv 2014-06-01 13:38:27 +05:30
parent 48679eeff8
commit 572debd0e8

View file

@ -587,26 +587,34 @@ class manager
$subscriptions = array(); $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 ($this->get_subscription_types() as $group_name => $types)
{ {
foreach ($types as $id => $type) 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 (empty($rows[$id]))
if (!$row)
{ {
// No rows at all, default to '' // No rows at all, default to ''
$subscriptions[$id] = array(''); $subscriptions[$id] = array('');
} }
else else
{ {
do foreach ($rows[$id] as $row)
{ {
if (!$row['notify']) if (!$row['notify'])
{ {
@ -620,10 +628,7 @@ class manager
$subscriptions[$id][] = $row['method']; $subscriptions[$id][] = $row['method'];
} }
while ($row = $this->db->sql_fetchrow($result));
} }
$this->db->sql_freeresult($result);
} }
} }