The fORMJSON class is an ORM plugin to provide JSON encoding functionality to fActiveRecord and fRecordSet objects.
To add the JSON encoding functionality for whole records and record sets, simply call the static method extend()
.
fORMJSON::extend();
The fORMJSON class adds a toJSON()
method to both fActiveRecord and fRecordSet. Calling toJSON()
on a record will create a single JSON object with each column being the property name and the value being the value. Calling toJSON()
on a record set will create an array of the record JSON objects.
$user = new User(3);
echo $user->toJSON();
$users = fRecordSet::build('User');
echo $users->toJSON();
When outputting JSON to a browser, it is best practice to include the appropriate Content-Type
header by calling fJSON::sendHeader().
fJSON::sendHeader();
$user = new User(3);
echo $user->toJSON();