You are viewing documentation for v1.0.x. Change

Automatic Time Stamps

Let Wheels handle time stamping of records.

When working with database tables, it is very common to have a column that holds the time that the record was added or last modified. If you have an e-commerce website with an orders table, you want to store the date and time the order was made; if you run a blog, you want to know when someone left a comment; and so on.

As with anything that is a common task performed by many developers, it makes a good candidate for abstracting to the framework level. So that's what we did.

Columns Used for Timestamps

If you have either of the following columns in your database table, Wheels will see them and treat them a little differently than others.

createdAt

Wheels will use a createdAt column automatically to store the current date and time when an INSERT operation is made (which could happen through a save() or create() operation, for example).

updatedAt

If Wheels sees an updatedAt column, it will use it to store the current date and time automatically when an UPDATE operation is made (which could happen through a save() or update() operation, for example).

Data Type of Columns

If you add any of these columns to your table, make sure they can accept date/time values (like datetime or timestamp, for example) and that they can be set to null.

^ Top
Table of Contents

Comments

Read and submit questions, clarifications, and corrections about this chapter.

[Add Comment]

  1. Chris Martinez's Gravatar Chris Martinez says:

    Can we drop the 'At' in the names to just 'created' and 'modified'?

  2. Chris Peters's Gravatar Chris Peters says:

    Chris, you can do so by adding these configurations to your file at config/settings.cfm:

    <cfset set(timeStampOnCreateProperty="created")>
    <cfset set(timeStampOnUpdateProperty="modified")>

  3. The page about naming conventions says "In your database, both table names and column names should be lowercase. The customersegments table could have fields called title, regionid, and incomelevel, for example."

    However, this page seems to indicate the createdAt and updatedAt columns in my database should use camel case. Should they really be createdat and updatedat? Also should foreign key references be camel case i.e. userId or all lowercase i.e. userid?

  4. Troy Murray's Gravatar Troy Murray says:

    Chris T,

    The columns should all be lowercase, not camel case.  If you're using the DBMigrate plugin (after version 0.4.0) the columns will be all lowercase, prior to that they were camelCase.  Me thinks this document needs to be updated to show them in all lowercase.

  5. Brian Lang's Gravatar Brian Lang says:

    I've got fields called createdat and modifiedat but I'm not seeing them get updated on creation or update.
    Database is MySQL.
    Fields are set as datetime, Allow Null is set, and the default value = NULL

    In my controller, In my edit method, I am calling create()
    <cfset horse = model("horse").create(params.horse)>
    And in my update method, I am calling update()
    <cfset horse.update(params.horse)>

    So why am I not seeing the database fields update appropriately?

Add Comment