Mapping your object from an array
Simple usage
In its simplest form, this method takes an array of key=>values and map them to the object properties.</p>
Available options
The fromArray method accept an optional second parameter in the form of an array to alter its behavior. The following properties are available:</p>
- load_assocations
If true, the object will be initialised with its associations, eliminating the need to call the get functions to fill the record with its associations - relax
If true, every field present in the array parameter will be mapped to the object, even if not defined in meta. - dont_encode_password
Disable password encoding. - encoding
Define the encoding of the strings present in the source array.
Exemples on using the fromArray method in its simplest form</p>
$userArray = array(
'id'=>34,
'lastname'=>'my lastname',
'firstname'=>'my firstname'
);
// initialize the user object
$user = new User();
$user->fromArray($userArray);
// persist the user object
$user->save();
Importing associations
Moreover, the fromArray method will preload the associations related to the object if it find the relevant keys corresponding to the one present in the field_meta configuration array. Be carefull to not overwrite your existing associations, or look at the 'load_assocations' option key describe below to enable/disable this feature.</p>
Exemples on using the fromArray method with associations:</p>
$fileArray = array(
'id'=>56,
'name'=>'my file',
'users'=>'61,98'
);
$file = new File();
$file->fromArray($fileArray);
$file->save();
print_r($file);
/* will print:
File Object
(
[meta_table] => Array
(
...
)
[meta_fields] => Array
(
...
)
[con] => Resource id #453
[attributes] => Array
(
[name] => my name
)
[associations] => Array
(
[users] => Array
(
[0] => User Object
(
[meta_table] => Array
(
...
)
[meta_fields] => Array
(
...
)
[con] => Resource id #453
[attributes] => Array
(
...
)
[associations] => Array
(
...
)
)
[1] => User Object
(
[meta_table] => Array
(
...
)
[meta_fields] => Array
(
)
[con] => Resource id #453
[attributes] => Array
(
...
)
[associations] => Array
(
...
)
)
)
)
)
*/