From 99c481897b8d1574bf984a1d5ea732fe63482fe0 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Sat, 20 Dec 2008 22:49:32 +0000 Subject: Add "created" and "updated" timestamps to the items table. --- core/helpers/core_installer.php | 2 ++ core/models/item.php | 13 +++++++++ core/tests/Item_Model_Test.php | 63 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 core/tests/Item_Model_Test.php (limited to 'core') diff --git a/core/helpers/core_installer.php b/core/helpers/core_installer.php index 4ab54da9..399b6bf5 100644 --- a/core/helpers/core_installer.php +++ b/core/helpers/core_installer.php @@ -44,6 +44,7 @@ class core_installer { ENGINE=InnoDB DEFAULT CHARSET=utf8;"); $db->query("CREATE TABLE `items` ( + `created` int(9) default NULL, `description` char(255) default NULL, `height` int(9) default NULL, `id` int(9) NOT NULL auto_increment, @@ -60,6 +61,7 @@ class core_installer { `thumb_width` int(9) default NULL, `title` char(255) default NULL, `type` char(32) NOT NULL, + `updated` int(9) default NULL, `view_count` int(9) default 0, `width` int(9) default NULL, PRIMARY KEY (`id`), diff --git a/core/models/item.php b/core/models/item.php index e5eb4039..d71414d8 100644 --- a/core/models/item.php +++ b/core/models/item.php @@ -237,4 +237,17 @@ class Item_Model extends ORM_MPTT { return parent::__get($column); } } + + /** + * @see ORM::save() + */ + public function save() { + if (!empty($this->changed) && $this->changed != array("view_count" => "view_count")) { + $this->updated = time(); + if (!$this->loaded) { + $this->created = $this->updated; + } + } + return parent::save(); + } } diff --git a/core/tests/Item_Model_Test.php b/core/tests/Item_Model_Test.php new file mode 100644 index 00000000..6024366b --- /dev/null +++ b/core/tests/Item_Model_Test.php @@ -0,0 +1,63 @@ +name = rand(); + $item->save(); + $this->assert_true(!empty($item->created)); + $this->assert_true(!empty($item->updated)); + } + + public function updating_doesnt_change_created_date_test() { + $item = ORM::factory("item"); + $item->name = rand(); + $item->save(); + + // Force the creation date to something well known + $db = Database::instance(); + $db->query("UPDATE `items` SET `created` = 0 WHERE `id` = $item->id"); + $db->query("UPDATE `items` SET `updated` = 0 WHERE `id` = $item->id"); + $item->reload(); + $item->title = "foo"; // force a change + $item->save(); + + $this->assert_true(empty($item->created)); + $this->assert_true(!empty($item->updated)); + } + + public function updating_view_count_only_doesnt_change_updated_date_test() { + $item = ORM::factory("item"); + $item->name = rand(); + $item->save(); + $item->reload(); + $this->assert_same(0, $item->view_count); + + // Force the updated date to something well known + $db = Database::instance(); + $db->query("UPDATE `items` SET `updated` = 0 WHERE `id` = $item->id"); + $item->reload(); + $item->view_count++; + $item->save(); + + $this->assert_same(1, $item->view_count); + $this->assert_true(empty($item->updated)); + } +} -- cgit v1.2.3