Doctrine: DATETIME default NOW()

By , Wednesday 30th December 2009 6:30 pm

I’ve been struggling with setting up a database schema for a new Zend Framework project. I’m using trying to use Doctrine ORM for my database models. I need to set up the schema so that it allowed me to set a default date and time for a `datetime` column, e.g. when adding a new message I get the current timestamp. After much searching and experimenting I found the solution so I’m sharing it.

In your schema YAML file simply do the following:

Message:
  actAs: 
    Timestampable:
      created:
        name: created_at
        type: timestamp
        format: Y-m-d H:i:s
      updated:
        name: last_updated
        type: timestamp
        format: Y-m-d H:i:s    
  columns:
    id:
      type: integer
      primary: true
      autoincrement: true
    name: string(255)
    email: string(300)
    message: string(2000)

If on the other hand you don’t want an `updated_at` column you can use the following:

Message:
  actAs: 
    Timestampable:
      created:
        name: created_at
        type: timestamp
        format: Y-m-d H:i:s
      updated:
        disabled: true
  columns:
    id:
      type: integer
      primary: true
      autoincrement: true
    name: string(255)
    email: string(300)
    message: string(2000)

Liked this post? Follow this blog to get more. 

6 Responses to “Doctrine: DATETIME default NOW()”

  1. Thomas Coleman says:

    That worked a treat for me :D thanks for that, i had seen something like this when looking at doctrine examples but when i actually wanted to do it i could not find anything about it hehe.

  2. Misael says:

    Thank you very much for your work.

  3. Brady says:

    I think you want format: Y-m-d H:i:s

  4. Thanks for pointing that out, its been corrected in live code, but obviously not here :)

Leave a Reply

You must be logged in to post a comment.

Panorama Theme by Themocracy

2 visitors online now
1 guests, 1 bots, 0 members
Max visitors today: 4 at 12:53 am UTC
This month: 16 at 07-08-2017 06:57 am UTC
This year: 45 at 02-01-2017 10:28 pm UTC
All time: 130 at 28-03-2011 10:40 pm UTC