0

I'm converting some old procedural code to OOP.

Lets say I have some procedural code that marks a record as trashed and records the time that is done.

UPDATE
toys
SET
is_trashed = 1,
trashed_date_timestamp = NOW()
WHERE
id = ??

I gather, from the various books on OOP, that in the object oriented paradigm you:

  1. 'find' a specific item as an object, and then
  2. modify its properties, and then
  3. save its properties to the db using a generic 'update' method on an ORM mapper

This is in contrast to the procedural/db-centric method used in my code where I run an SQL query to update a specific record with specific values in the db for a specific change of properties.

So, lets say I wanted to convert this code to the Object Oriented paradigm.

  1. I find my object by its id, and
  2. modify its properties:
    • I set the isTrashed property to TRUE
    • I set the trashedDateTimestamp property to ???

huh!?

When I try to set the trashedDateTimestamp property to NOW its opens up a whole load of issues.

  • What is now? I'd prefer to ensure that it is the NOW() from the database time, not NOW on the server.
  • How do clients of the toy object handle getting and setting that trashedDateTimestamp property?

I can think of lots of ways to 'skin this cat' but would like to know if there are any patterns that solve this issue.

Or is this feature proven to be impossible in the OO Paradigm and I should use something like an 'events observer' instead.

In short - How do you store NOW onto object properties with reasonable accuracy?

JW.
  • 4,821
  • 5
  • 43
  • 60

1 Answers1

1

You store a DateTime object without any passed parameters. The DateTime object would default to the current time.

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
  • I think I see. So that DateTime object is somehow synched to the time used on the database? – JW. Dec 04 '12 at 17:26
  • 1
    @JW.: No. Because you shouldn't really mind about the MySQL time (because it's not the one running your application, but the server is), store all of the times as server times and be over with it. – Madara's Ghost Dec 04 '12 at 17:29
  • Thanks. The _database time_ is one of the last db-centric things that I'm clinging onto during my steady conversion to the OOP way of working. I shall have some counselling to help me let go of it. :o) – JW. Dec 04 '12 at 17:37
  • 1
    Details differ depending on what ORM you are using. But, in general, the idea is that if you have an object with a Date/Time property, and you have a table with a corresponding column. In this case, if you don't provide the value for this column during saving, it automatically defaults to database time, at the time of save/update. – Nazar Merza Dec 04 '12 at 18:54
  • @NazarMerza That sounds interesting. You can expand on that as an answer maybe? – JW. Dec 04 '12 at 20:11