Convertion between a json string and a record

The Json plugin is act as a shortcut to the "fromArray" and "toArray" methods. It provides two new methods which are "fromJson" and "toJson" which expect the same arguments as their respective counterpart.

Internally, those methods use the native PHP functions "json_encode" and "json_decode", which strictly expect a Json formatted string. In simpler words, it meens no trailing commas and keys surrounded by double quotes.

Method "fromJson"

It is a simple wrapper around the "fromArray" method with which it share all its arguments and behaviors with the difference that it expect a JSON formated string instead of an array as first argument.

For more information, please refer to the fromArray method documentation.

Method "toJson"

It is a simple wrapper around the "toArray" method with which it share all its arguments and behaviors with the difference that it return a JSON formated string instead of an array.

For more information, please refer to the toArray method documentation.

Exemple

Exemples on using the "fromJson" method to update an existing record and then on using the "toJson" method to return the same record as a json formated string.


$json = '{"id":34,"lastname":"my lastname","firstname":"my firstname"}';

// Update the user with id "34"
$user = new User();
$user->fromArray($json);
$user->save();

// Return the same user as a json string
$user = new User(34);
echo $user->toJson();

Open Source Object Relational Mapping in PHP

Download Porte 0.2.1