Doctrine ORM – Saving MySQL DateTime Format ‘NOW()’ into a Record

I ran into an issue with a column type of ‘timestamp’ in one of my models. If I did not specifically set the date field, it would save it as a timestamp and throw things off in the database.

So I searched around a bit and found the following solution:

$newUser = new Model_DbTable_User();
$newUser ->fromArray($_POST);
$newUser ->date = new Doctrine_Expression('NOW()');

‘date’ being a column inside of the User model that is type ‘timestamp’. I am utilizing the fromArray method in order to inject my form values directly into the database (so nice!). The fix that I am talking about is the 3rd line of code. In order to save with the datetime format, you must specify the Doctrine Expression as shown here. Works like a charm.

Tuesday, October 27th, 2009 Doctrine

2 Comments to Doctrine ORM – Saving MySQL DateTime Format ‘NOW()’ into a Record

  • Darren says:

    Thanks for this. It helped me a lot! Couldn’t find it anywhere else!

  • admin says:

    Glad it helped – you’re welcome!

  • Leave a Reply

    You must be logged in to post a comment.

    [ad code=1 align=center]