Skip to content

Commit 6a3e247

Browse files
committed
dbObject: added JsonBuilder()
1 parent cf5fd70 commit 6a3e247

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

dbObject.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ Second parameter is 'required' and its defines that following entry field be alw
239239

240240
NOTE: All variables which are not defined in the $dbFields array will be ignored from insert/update statement.
241241

242-
###Array as return values
242+
###Using array as a return value
243243
dbObject can return its data as array instead of object. To do that ArrayBuilder() function should be used in the beginning of the call.
244244
```php
245245
$user = user::ArrayBuilder()->byId (1);
@@ -251,11 +251,16 @@ dbObject can return its data as array instead of object. To do that ArrayBuilder
251251
```
252252

253253
Following call will return data only of the called instance without any relations data. Use with() function to include relation data as well.
254-
255254
```php
256255
$user = user::ArrayBuilder()->with ("product")->byId (1);
257256
print_r ($user['products']);
258257
```
258+
259+
###Using json as a return value
260+
Togeather with ArrayBuilder() and ObjectBuilder() dbObject can return result in json format to avoid extra coding
261+
```php
262+
$userjson = user::JsonBuilder()->with ("product")->byId (1);
263+
```
259264
###Object serialization
260265

261266
Object could be easily converted to a json string or an array.

dbObject.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,16 @@ public function __unset ($name) {
170170
unset ($this->data[$name]);
171171
}
172172

173+
/**
174+
* Helper function to create dbObject with Json return type
175+
*
176+
* @return dbObject
177+
*/
178+
public static function JsonBuilder () {
179+
$obj = new static;
180+
$obj->returnType = 'Json';
181+
return $obj;
182+
}
173183

174184
/**
175185
* Helper function to create dbObject with Array return type
@@ -299,6 +309,8 @@ private function getOne ($fields = null) {
299309
$this->processArrays ($results);
300310
$this->data = $results;
301311
$this->processWith ($results);
312+
if ($this->returnType == 'Json')
313+
return json_encode ($results);
302314
if ($this->returnType == 'Array')
303315
return $results;
304316

@@ -334,6 +346,9 @@ private function get ($limit = null, $fields = null) {
334346
if ($this->returnType == 'Object')
335347
return $objects;
336348

349+
if ($this->returnType == 'Json')
350+
return json_encode ($results);
351+
337352
return $results;
338353
}
339354

0 commit comments

Comments
 (0)