Simple way to auto-update datetime column every time a Content Part record is updated?

Topics: Customizing Orchard, General, Writing modules
Feb 29, 2012 at 9:03 PM

I like to add "created_at", "updated_at" columns to my db tables, and default them to current_timestamp. I want updated_at to always get updated with the current_timestamp when the record is updated, but to keep created_at's original value once it's been set. I normally do this either through stored procs or triggers. 

I know I could achieve similar results by putting a CommonPart onto the containing type, but that's not always easy to use, and requires an additional join (and the table names in Orchard db's are pretty verbose). 

Is there a simple way to do this with content part records in Orchard? Can you add triggers via Migrations.cs? 

Feb 29, 2012 at 9:34 PM

You could try to update it in a handler: add an event handler for a part (like OnEditorUpdate or what is it called; Intellisense will throw them up if you start typing "On" in the handler's ctor) or for a content type by overriding the handler's base's methods (and don't forget to check inside the method if the current item it runs for is of the type you want to process).

Mar 1, 2012 at 4:37 PM

As Piedone suggest, you can also copy the CommonPart handler to mimic the behavior. Though using CommonPart would be the best approach. Don't do it for the sake of a single join which is nothing in the whole request work. And if you care about performance, use server side caching, and output caching.

Mar 1, 2012 at 4:53 PM

I'm only concerned about the joins because I query the database directly when I pull down data from the "content" environment where my editors are writing content. I also query the db a lot to auto generate stubs for new content items related to content the editors have created in "content" env. So yeah, mainly just for manual db querying. It's easier to query a table than to join the tables with these verbose names. I'll probably just make some views to make this easier.