Skip to content

Type Normalization

Romans Malinovskis edited this page Aug 19, 2016 · 10 revisions

Agile Data recognizes some of the basic types. All persistence drivers are requested to properly handle those types. For instance a type 'date' will be set using a PHP's default date type. When storing into MySQL this must be converted to the native format (stirng). On other hand, if that is saved into 'MongoDB' then MongoDate must be used instead.

Types that are normalized by Agile Data

type alias(es) description native sql mongo
int integer will cast to int make sure it's not passed as a string. -394, "49" 49 49
float decimal number with floating point 3.2884,
money Will convert loosly-specified currency into float or dedicated format for storage. Optionally support 'fmt' proprety. "£3,294.48", 3.99999 38294.48, 4
bool boolean true / false type value. Optionally specify 'enum'=>['Y','N'] to store true as 'Y' and false as 'N'. By default uses [1,0]. true 1 true
array Optionally pass 'fmt' option, which is 'json' by default. Will json_encode and json_decode(..., true) the value if database does not support array storage. [2=>"bar"] {2:"bar"} stored as-is
binary Supports storage of binary data
string Will be trim() ed.

Converting

When loading data from pesistence, driver will always ensure that model has the date set in a native PHP format. For instance when you set a "money", then the model will contain a floating number.

If you do specify an unusual value inside a model, e.g. set money to "£3,123", then persistence driver will automatically convert this format into "3123.00" before storing it. The loaded value will also be "3123.00".

It's suggested that if the data is presented in a non-standard format, you should parse it while leading user input and convert it accordingly.

Dates and Time

TODO: need some research on storing dates / times

There are 3 date formats supported:

  • ts (or timestamp): Stores in database using UTC. Defaults into unix timestamp (int) in PHP.

  • date: Converts into YYYY-MM-DD using UTC timezone for SQL. Defaults to DateTime() class in PHP, but supports string input (parsed as date in a current timezone) or unix timestamp.

  • time: converts into HH:MM:SS using UTC timezone for storing in SQL. Defaults to DateTime() class in PHP, but supports string input (parsed as date in current timezone) or unix timestamp. Will discard date from timestamp.

  • datetime: stores both date and time. Uses UTC in DB. Defaults to DateTime() class in PHP. Supports string input parsed by strtotime() or unix timestamp.

WARNING

Type normalization is NOT designed for type formatting.