From 71b810b7f4b15556bb5979154f8cf1ec45541ecb Mon Sep 17 00:00:00 2001 From: Koen Punt Date: Fri, 23 Jan 2015 13:02:15 +0100 Subject: [PATCH 1/2] add touch method to model --- lib/Model.php | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/lib/Model.php b/lib/Model.php index 76a1e399a..f9456534b 100644 --- a/lib/Model.php +++ b/lib/Model.php @@ -1270,6 +1270,33 @@ public function set_timestamps() $this->created_at = $now; } + /** + * Touch a model, updates `updated_at` and any other column given as argument. + * + * Please note that no validation is performed and only the after_touch, after_commit and after_rollback callbacks are executed. + * + * @param string|array $keys... + * @return bool True if update succeeds + * @throws ActiveRecord\ActiveRecordException if object is a new record + */ + public function touch($keys = array() /*[, $key...] */) + { + if($this->is_new_record()) + throw new ActiveRecordException('Cannot touch on a new record object'); + + if(!is_array($keys)) + $keys = func_get_args(); + + if(!in_array('updated_at', $keys)) + $keys[] = 'updated_at'; + + $now = date('Y-m-d H:i:s'); + $attributes = array_fill_keys($keys, $now); + + $this->set_attributes($attributes); + return $this->update(false); + } + /** * Mass update the model with attribute data and saves to the database. * From 5f13e8ebf52e43e34ea6457c3c3eb9dde3998b74 Mon Sep 17 00:00:00 2001 From: Koen Punt Date: Tue, 13 Jun 2017 23:24:37 +0200 Subject: [PATCH 2/2] add some tests --- test/ActiveRecordWriteTest.php | 42 ++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/test/ActiveRecordWriteTest.php b/test/ActiveRecordWriteTest.php index c0351413d..c84d6dbe1 100644 --- a/test/ActiveRecordWriteTest.php +++ b/test/ActiveRecordWriteTest.php @@ -441,4 +441,46 @@ public function test_update_our_datetime() $this->assert_true($our_datetime === $author->some_date); } + public function test_touch() + { + $author = Author::create(array('name' => 'MC Hammer')); + $updated_at = $author->updated_at = new DateTime('yesterday'); + $author->save(); + $author->touch(); + $this->assertGreaterThan($updated_at, $author->updated_at); + } + + /** + * @expectedException ActiveRecord\ActiveRecordException + * @expectedExceptionMessage Cannot touch on a new record object + */ + public function test_touch_on_new_record() + { + $author = new Author(array('name' => 'MC Hammer')); + $author->touch(); + } + + public function test_touch_with_additional_keys() + { + $author = Author::create(array('name' => 'MC Hammer')); + $updated_at = $author->updated_at = new DateTime('yesterday'); + $some_date = $author->some_date = new DateTime('yesterday'); + $author->save(); + $author->touch(array('some_date')); + $this->assertGreaterThan($updated_at, $author->updated_at); + $this->assertGreaterThan($some_date, $author->some_date); + } + + public function test_touch_with_scalar() + { + $author = Author::create(array('name' => 'MC Hammer')); + $updated_at = $author->updated_at = new DateTime('yesterday'); + $some_date = $author->some_date = new DateTime('yesterday'); + $author->save(); + $author->touch('some_date'); + $this->assertGreaterThan($updated_at, $author->updated_at); + $this->assertGreaterThan($some_date, $author->some_date); + } + + }