-
Notifications
You must be signed in to change notification settings - Fork 46
Type Normalization
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.
type | alias(es) | description | native | sql | mongo |
---|---|---|---|---|---|
string | Will be trim() ed. | ||||
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 like BLOBs |
- Money: http://php.net/manual/en/numberformatter.parsecurrency.php.
- money: See also http://www.thefinancials.com/Default.aspx?SubSectionID=curformat
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.
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.
Type normalization is NOT designed for type formatting.
Persistence driver will walk through requested fields and will construct a transformation string
:
$tr = [ 'field'=>fx(), 'field'=>fx(), ..]
After reading each row of data Persistence will iterate to apply the conversion. If type is omitted, not supported or does not have to be converted (e.g. string), then $tr won't contain the key for the field.
$m->addField('myfield', [
'type'=>'money',
'fmt'=> new NumberFormatter('de_DE', NumberFormatter::CURRENCY)
]);
or
$m->addField('myfield', [
'type'=>'money',
'fmt'=>'de_DE'
]);
You can override the formatting function by specifying it to the field:
$m->addField('myfield', ['load_parse'=>function($value){ return (int)$value; }]);
Persistence driver does not care what you set inside your model. Validators can deal with that. But when you request to save your active record, it will change format of fields.
You can specify format like this:
$m->addField('myfield', ['load_parse'=>function($value){ return (int)$value; }]);
Agile Data, Documentation and Wiki content is licensed under MIT and (c) by Agile Toolkit Limited UK