How to specify defaultValue to "now" for a timestamp column in Propel?
Asked Answered
A

3

5

Here is my table definition for Propel schema. Column modified has timestamp type (DateTime in PHP) and I'd like to assign a default value to now. I've tried setting it to "now" but I'm getting an error using propel-gen insert-sql command:

"Syntaxt error or access violation: 1067 Invalid default value for 'modified'.

Anyone knows how I can set default value to now for a timestamp column in Propel?

<table name="mashup_setting">
   <!-- omitted previous column definition -->
   <column name="modified" type="timestamp" required="true" defaultValue="now" />
</table>
Allgood answered 6/2, 2012 at 19:32 Comment(0)
O
5

Change the name to "updated_at" which is a special field in Propel that will automatically be updated to NOW() whenever you update the field. "created_at" is also similar and will do the same thing when your object is created.

In your model you can always proxy "modified" or getModified() to getUpdatedAt() to complete your functionality.

If you have to have "modified" as the name for your column, you need to write a behavior, which I think is more work that you need to accomplish this. You can find information about behaviors here.

http://www.symfony-project.org/cookbook/1_2/en/behaviors

Overtime answered 6/2, 2012 at 20:51 Comment(0)
S
15

In newer Propel versions you can specify you column like so:

<column name="created" type="TIMESTAMP" defaultExpr="CURRENT_TIMESTAMP" />

It's then cross-db compatible.

Sinclair answered 7/2, 2014 at 17:6 Comment(2)
This should be the accepted answer as it is more accurate!Edmundedmunda
Agreed, this is more accurate and should be the accepted answerRetrogressive
O
5

Change the name to "updated_at" which is a special field in Propel that will automatically be updated to NOW() whenever you update the field. "created_at" is also similar and will do the same thing when your object is created.

In your model you can always proxy "modified" or getModified() to getUpdatedAt() to complete your functionality.

If you have to have "modified" as the name for your column, you need to write a behavior, which I think is more work that you need to accomplish this. You can find information about behaviors here.

http://www.symfony-project.org/cookbook/1_2/en/behaviors

Overtime answered 6/2, 2012 at 20:51 Comment(0)
R
2

The docs at http://propelorm.org/behaviors/timestampable.html don't specify if this is available in 1.6 but add this to schema and build:

<behavior name="timestampable" />

The model now has two new columns, created_at and updated_at, that store a timestamp automatically updated on save:

$obj->save();
echo $obj->getCreatedAt(); // 2009-10-02 18:14:23
echo $obj->getUpdatedAt(); // 2009-10-02 18:14:25
Rockefeller answered 10/4, 2014 at 17:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.