diff options
Diffstat (limited to 'modules/tag/models')
| -rw-r--r-- | modules/tag/models/tag.php | 21 |
1 files changed, 14 insertions, 7 deletions
diff --git a/modules/tag/models/tag.php b/modules/tag/models/tag.php index bd665667..13e253ba 100644 --- a/modules/tag/models/tag.php +++ b/modules/tag/models/tag.php @@ -69,15 +69,22 @@ class Tag_Model_Core extends ORM { * to this tag. */ public function save() { - $related_item_ids = array(); - foreach (db::build() - ->select("item_id") - ->from("items_tags") - ->where("tag_id", "=", $this->id) - ->execute() as $row) { - $related_item_ids[$row->item_id] = 1; + // Check to see if another tag exists with the same name + $duplicate_tag = ORM::factory("tag") + ->where("name", "=", $this->name) + ->where("id", "!=", $this->id) + ->find(); + if ($duplicate_tag->loaded()) { + // If so, tag its items with this tag so as to merge it. + foreach ($duplicate_tag->items() as $item) { + $this->add($item); + } + + // ... and remove the duplicate tag + $duplicate_tag->delete(); } + // Figure out what items have changed in this tag for our item_related_update event below if (isset($this->object_relations["items"])) { $added = array_diff($this->changed_relations["items"], $this->object_relations["items"]); $removed = array_diff($this->object_relations["items"], $this->changed_relations["items"]); |
