Skip to content

Commit

Permalink
add touch method to model
Browse files Browse the repository at this point in the history
  • Loading branch information
koenpunt committed Jun 13, 2017
1 parent d5f1895 commit b7ee409
Showing 1 changed file with 27 additions and 0 deletions.
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

0 comments on commit b7ee409

Please sign in to comment.