Commit 6a3e2477 authored by Alexander Butenko's avatar Alexander Butenko

dbObject: added JsonBuilder()

parent cf5fd70b
...@@ -239,7 +239,7 @@ Second parameter is 'required' and its defines that following entry field be alw ...@@ -239,7 +239,7 @@ Second parameter is 'required' and its defines that following entry field be alw
NOTE: All variables which are not defined in the $dbFields array will be ignored from insert/update statement. NOTE: All variables which are not defined in the $dbFields array will be ignored from insert/update statement.
###Array as return values ###Using array as a return value
dbObject can return its data as array instead of object. To do that ArrayBuilder() function should be used in the beginning of the call. dbObject can return its data as array instead of object. To do that ArrayBuilder() function should be used in the beginning of the call.
```php ```php
$user = user::ArrayBuilder()->byId (1); $user = user::ArrayBuilder()->byId (1);
...@@ -251,11 +251,16 @@ dbObject can return its data as array instead of object. To do that ArrayBuilder ...@@ -251,11 +251,16 @@ dbObject can return its data as array instead of object. To do that ArrayBuilder
``` ```
Following call will return data only of the called instance without any relations data. Use with() function to include relation data as well. Following call will return data only of the called instance without any relations data. Use with() function to include relation data as well.
```php ```php
$user = user::ArrayBuilder()->with ("product")->byId (1); $user = user::ArrayBuilder()->with ("product")->byId (1);
print_r ($user['products']); print_r ($user['products']);
``` ```
###Using json as a return value
Togeather with ArrayBuilder() and ObjectBuilder() dbObject can return result in json format to avoid extra coding
```php
$userjson = user::JsonBuilder()->with ("product")->byId (1);
```
###Object serialization ###Object serialization
Object could be easily converted to a json string or an array. Object could be easily converted to a json string or an array.
......
...@@ -170,6 +170,16 @@ class dbObject { ...@@ -170,6 +170,16 @@ class dbObject {
unset ($this->data[$name]); unset ($this->data[$name]);
} }
/**
* Helper function to create dbObject with Json return type
*
* @return dbObject
*/
public static function JsonBuilder () {
$obj = new static;
$obj->returnType = 'Json';
return $obj;
}
/** /**
* Helper function to create dbObject with Array return type * Helper function to create dbObject with Array return type
...@@ -299,6 +309,8 @@ class dbObject { ...@@ -299,6 +309,8 @@ class dbObject {
$this->processArrays ($results); $this->processArrays ($results);
$this->data = $results; $this->data = $results;
$this->processWith ($results); $this->processWith ($results);
if ($this->returnType == 'Json')
return json_encode ($results);
if ($this->returnType == 'Array') if ($this->returnType == 'Array')
return $results; return $results;
...@@ -334,6 +346,9 @@ class dbObject { ...@@ -334,6 +346,9 @@ class dbObject {
if ($this->returnType == 'Object') if ($this->returnType == 'Object')
return $objects; return $objects;
if ($this->returnType == 'Json')
return json_encode ($results);
return $results; return $results;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment