Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add touch method to model #488

Open
wants to merge 2 commits into
base: 1.1-dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions lib/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
42 changes: 42 additions & 0 deletions test/ActiveRecordWriteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


}