Skip to content

Commit

Permalink
Merge pull request #70 from cgroner/3.2/develop
Browse files Browse the repository at this point in the history
Document auto update/create columns.
  • Loading branch information
kemo committed Apr 10, 2014
2 parents 4af8c37 + 158466f commit ece5244
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
11 changes: 7 additions & 4 deletions classes/kohana/orm.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,17 @@ public static function factory($model, $id = NULL)
protected $_table_columns;

/**
* Auto-update columns for updates
* @var string
* Auto-update column name & format for updates.
* @see $_created_column
* @var mixed An `array` with 'column' and 'format' keys and values or NULL for no auto update column
*/
protected $_updated_column = NULL;

/**
* Auto-update columns for creation
* @var string
* Auto-update column name & format for creation. If 'format' is TRUE, the
* php 'time()' method will be used, otherwise the 'format' will be passed
* as the format parameter to the php 'date()' function.
* @var mixed An `array` with 'column' and 'format' keys and values or NULL for no auto update column
*/
protected $_created_column = NULL;

Expand Down
19 changes: 19 additions & 0 deletions guide/orm/models.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,22 @@ ORM assumes each model (and database table) has an `id` column that is indexed a
For each model, you can define which database configuration ORM will run queries on. If you override the `$_db_group` variable in your model, ORM will connect to that database. Example:

protected $_db_group = 'alternate';

## Auto-update columns

If you'd like date/time columns automatically updated whenever your model is
changed or created you can define the `$_created_column` and/or
`$_updated_column` properties in your model. Use an `array` with 'column'
and 'format' keys & values, where 'column' is the column name to be updated
and 'format' is the date format to use. You can set 'format' to TRUE
for a Unix timestamp, or specify a string format like that expected for the
php `date()` function.

The following example will automatically update the 'last_changed' column
with the current Unix timestamp whenever the model is updated:

class Model_Member extends ORM
{
protected $_updated_column = array('column' => 'last_changed',
'format' => TRUE);
}

0 comments on commit ece5244

Please sign in to comment.