Skip to content

Commit 71b810b

Browse files
committed
add touch method to model
1 parent d5f1895 commit 71b810b

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

lib/Model.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,6 +1270,33 @@ public function set_timestamps()
12701270
$this->created_at = $now;
12711271
}
12721272

1273+
/**
1274+
* Touch a model, updates `updated_at` and any other column given as argument.
1275+
*
1276+
* Please note that no validation is performed and only the after_touch, after_commit and after_rollback callbacks are executed.
1277+
*
1278+
* @param string|array $keys...
1279+
* @return bool True if update succeeds
1280+
* @throws ActiveRecord\ActiveRecordException if object is a new record
1281+
*/
1282+
public function touch($keys = array() /*[, $key...] */)
1283+
{
1284+
if($this->is_new_record())
1285+
throw new ActiveRecordException('Cannot touch on a new record object');
1286+
1287+
if(!is_array($keys))
1288+
$keys = func_get_args();
1289+
1290+
if(!in_array('updated_at', $keys))
1291+
$keys[] = 'updated_at';
1292+
1293+
$now = date('Y-m-d H:i:s');
1294+
$attributes = array_fill_keys($keys, $now);
1295+
1296+
$this->set_attributes($attributes);
1297+
return $this->update(false);
1298+
}
1299+
12731300
/**
12741301
* Mass update the model with attribute data and saves to the database.
12751302
*

0 commit comments

Comments
 (0)