Posts tagged Timestamp
Doctrine (within Symfony) Timestampable
0For most tables there is usually a need to track when a row was created or updated. Doctrine allows for this type of behavior but in a different way. If your table has created_at and updated_at columns (or just one or the other) you can tell doctrine to automatically set the correct timestamp. The datatype I use for my created_at and updated_at columns are ‘datetime’ so my schema would look like:
... `created_at` datetime NOT NULL, `updated_at` datetime NOT NULL ...
To ensure that your columns are updated you will need to edit your config/doctrine/schemal.yml file and add the following lines:
Before
tableName: objects
columns:
id:
type: integer(4)
primary: true
autoincrement: true
name:
type: string(50)
notnull: true
After
tableName: objects
actAs:
Timestampable:
created:
name: created_at
type: timestamp
updated:
name: updated_at
type: timestamp
columns:
id:
type: integer(4)
primary: true
autoincrement: true
name:
type: string(50)
notnull: true
Now that you edited your schema file you will need to rebuild your model so that the php model files know that you will be updating these columns during saves:
php symfony doctrine:build-model
And as always, clear your cache so that your code will be using the latest version of model files:
php symfony cc
For more information on timestample /w doctrine, visit their website.