All of my models have created_at and updated_at and several have one or more date fields that will be edited by the user. For each I would like to store as unix time and on retrieve from the db I want to convert to users date format of choice (ie: mm/dd/yyyy) or whatever their settings dictate. I am struggling a bit with best way to handle this with observers, trying to use typing correctly.
I have my date related properties with a data_type of 'time_unix'
On save() the Typing observer is throwing a PHP error with:
Argument 1 passed to Orm\Observer_Typing::type_time_encode() must be an instance of Fuel\Core\Date, integer given
After doing some debugging I am seeing that the updated_at column is getting set correctly in the UpdatedAt => before_save Observer and its integer value is being passed into the type_time_decode function in Observer Typing:
public static function type_time_encode(\Fuel\Core\Date $var, array $settings)
Is anything off with how I have my properties and observers defined with time_unix? When I comment the data_type columns out the "instance of Fuel\Core\Date" errors go away, for instance with updated_at.
Whats a "best practice" in fuel for converting/unconverting date/time fields before/after save to make them human readable for input?
Outside of date/json/serialize fields, assuming I can just put together a custom observer that iterates over all the properties and does correct handling based on data_type property setting?
The typing observer expects a Date object as the source, not a string or anything else.
I've written a Observer_Date for this purpose, in the properties of the observer definition I define the model properties that should be observed, and the format they are in (using the formats of the Date class).
That makes sense for Date conversions, will create one of those.
With the error above in my test case its being thrown by the updated_at property. The UpdatedAt observer is setting the int timestamp value, then followed right behind it in the Typing observer for before_save its throwing the error.
Do I not want to flag created_at and updated_at fields as time_unix data types? When I comment that out and leave the created/updated at fields with no data_type Typing has no issues.
All date/time field types use the Date class, either as source or as destination. So if you define a field as "time_unix", it expects a Date object passed, which it will convert to a unix timestamp.
As these fields are filled in by createdat/updatedat observers, using either a timestamp or a mysql _ datetime string, the Typing observer can't do anything here.
If you want to define a type, use 'int' for unixtime, since that's what it is.