summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBharat Mediratta <bharat@menalto.com>2009-02-10 04:34:34 +0000
committerBharat Mediratta <bharat@menalto.com>2009-02-10 04:34:34 +0000
commit11226bdd3abdc664bece47b2b3011213499b5826 (patch)
treee2935b84023e498b06db0189feaf747312bb5a81
parentc7d004d3199b75c92f9ab065513af1c4f2d5aba5 (diff)
Simplify delete into guaranteed 3 queries.
-rw-r--r--modules/tag/helpers/tag_event.php19
1 files changed, 4 insertions, 15 deletions
diff --git a/modules/tag/helpers/tag_event.php b/modules/tag/helpers/tag_event.php
index ccb22df9..7192edec 100644
--- a/modules/tag/helpers/tag_event.php
+++ b/modules/tag/helpers/tag_event.php
@@ -49,22 +49,11 @@ class tag_event_Core {
}
static function item_before_delete($item) {
- $tags = ORM::factory("tag")
- ->join("items_tags", "tags.id", "items_tags.tag_id")
- ->join("items", "items.id", "items_tags.item_id")
- ->where("items_tags.item_id", $item->id)
- ->find_all();
-
- foreach($tags as $tag) {
- $tag->count--;
- if ($tag->count > 0) {
- $tag->save();
- } else {
- $tag->delete();
- }
- }
-
$db = Database::instance();
+ $db->query("UPDATE `tags` SET `count` = `count` - 1 WHERE `count` > 0 " .
+ "AND `id` IN (SELECT `tag_id` from `items_tags` WHERE `item_id` = $item->id)");
+ $db->query("DELETE FROM `tags` WHERE `count` = 0 AND `id` IN (" .
+ "SELECT `tag_id` from `items_tags` WHERE `item_id` = $item->id)");
$db->query("DELETE FROM `items_tags` WHERE `item_id` = $item->id;");
}