From 966dee8628293f78fbf9431281709ceba011d3c2 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Fri, 7 Jan 2011 23:38:08 -0800 Subject: Move tag count management into the model. Fixes #1586. --- modules/tag/helpers/tag.php | 2 -- modules/tag/models/tag.php | 16 +++++++++++++--- 2 files changed, 13 insertions(+), 5 deletions(-) (limited to 'modules/tag') diff --git a/modules/tag/helpers/tag.php b/modules/tag/helpers/tag.php index bcd3b0c0..7d5c643a 100644 --- a/modules/tag/helpers/tag.php +++ b/modules/tag/helpers/tag.php @@ -36,11 +36,9 @@ class tag_Core { $tag = ORM::factory("tag")->where("name", "=", $tag_name)->find(); if (!$tag->loaded()) { $tag->name = $tag_name; - $tag->count = 0; } $tag->add($item); - $tag->count++; return $tag->save(); } diff --git a/modules/tag/models/tag.php b/modules/tag/models/tag.php index c038f6d1..25df87e0 100644 --- a/modules/tag/models/tag.php +++ b/modules/tag/models/tag.php @@ -20,6 +20,15 @@ class Tag_Model_Core extends ORM { protected $has_and_belongs_to_many = array("items"); + public function __construct($id=null) { + parent::__construct($id); + + if (!$this->loaded()) { + // Set reasonable defaults + $this->count = 0; + } + } + /** * Return all viewable items associated with this tag. * @param integer $limit number of rows to limit result to @@ -69,11 +78,12 @@ class Tag_Model_Core extends ORM { $related_item_ids[$row->item_id] = 1; } + $added = array_diff($this->changed_relations["items"], $this->object_relations["items"]); + $removed = array_diff($this->object_relations["items"], $this->changed_relations["items"]); if (isset($this->changed_relations["items"])) { - $changed = array_merge( - array_diff($this->changed_relations["items"], $this->object_relations["items"]), - array_diff($this->object_relations["items"], $this->changed_relations["items"])); + $changed = array_merge($added, $removed); } + $this->count = count($this->object_relations["items"]) + count($added) - count($removed); $result = parent::save(); -- cgit v1.2.3