Commit 1fe43590 authored by Alexander Butenko's avatar Alexander Butenko

Merge pull request #255 from avbdr/master

Bugfixes an features
parents e0abe5b1 59cb2d40
......@@ -112,6 +112,14 @@ class MysqliDb
*/
protected $isSubQuery = false;
/**
* Return type: 'Array' to return results as array, 'Object' as object
* 'Json' as json string
*
* @var string
*/
public $returnType = 'Object';
/**
* Variables for query execution tracing
*
......@@ -211,6 +219,38 @@ class MysqliDb
$this->_bindParams = array(''); // Create the empty 0 index
$this->_query = null;
$this->_queryOptions = array();
$this->returnType = 'Array';
}
/**
* Helper function to create dbObject with Json return type
*
* @return dbObject
*/
public function JsonBuilder () {
$this->returnType = 'Json';
return $this;
}
/**
* Helper function to create dbObject with Array return type
* Added for consistency as thats default output type
*
* @return dbObject
*/
public function ArrayBuilder () {
$this->returnType = 'Array';
return $this;
}
/**
* Helper function to create dbObject with Object return type.
*
* @return dbObject
*/
public function ObjectBuilder () {
$this->returnType = 'Object';
return $this;
}
/**
......@@ -358,25 +398,26 @@ class MysqliDb
{
$res = $this->get ($tableName, 1, $columns);
if (is_object($res))
if ($res instanceof MysqliDb)
return $res;
if (isset($res[0]))
else if (is_array ($res) && isset ($res[0]))
return $res[0];
else if ($res)
return $res;
return null;
}
/**
* A convenient SELECT * function to get one value.
* A convenient SELECT COLUMN function to get a single column value from one row
*
* @param string $tableName The name of the database table to work with.
*
* @return array Contains the returned column from the select query.
* @return string Contains the value of a returned column.
*/
public function getValue($tableName, $column)
{
$res = $this->get ($tableName, 1, "{$column} as retval");
$res = $this->ArrayBuilder()->get ($tableName, 1, "{$column} as retval");
if (isset($res[0]["retval"]))
return $res[0]["retval"];
......@@ -769,6 +810,9 @@ class MysqliDb
{
$parameters = array();
$results = array();
// See http://php.net/manual/en/mysqli-result.fetch-fields.php
$mysqlLongType = 252;
$shouldStoreResult = false;
$meta = $stmt->result_metadata();
......@@ -780,13 +824,17 @@ class MysqliDb
$row = array();
while ($field = $meta->fetch_field()) {
if ($field->type == $mysqlLongType)
$shouldStoreResult = true;
$row[$field->name] = null;
$parameters[] = & $row[$field->name];
}
// avoid out of memory bug in php 5.2 and 5.3
// avoid out of memory bug in php 5.2 and 5.3. Mysqli allocates lot of memory for long*
// and blob* types. So to avoid out of memory issues store_result is used
// https://github.com/joshcam/PHP-MySQLi-Database-Class/pull/119
if (version_compare (phpversion(), '5.4', '<'))
if ($shouldStoreResult)
$stmt->store_result();
call_user_func_array(array($stmt, 'bind_result'), $parameters);
......@@ -794,9 +842,14 @@ class MysqliDb
$this->totalCount = 0;
$this->count = 0;
while ($stmt->fetch()) {
$x = array();
foreach ($row as $key => $val) {
$x[$key] = $val;
if ($this->returnType == 'Object') {
$x = new stdClass ();
foreach ($row as $key => $val)
$x->$key = $val;
} else {
$x = array();
foreach ($row as $key => $val)
$x[$key] = $val;
}
$this->count++;
array_push($results, $x);
......@@ -810,6 +863,9 @@ class MysqliDb
$totalCount = $stmt->fetch_row();
$this->totalCount = $totalCount[0];
}
if ($this->returnType == 'Json') {
return json_encode ($results);
}
return $results;
}
......
......@@ -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.
###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.
```php
$user = user::ArrayBuilder()->byId (1);
......@@ -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.
```php
$user = user::ArrayBuilder()->with ("product")->byId (1);
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 could be easily converted to a json string or an array.
......
......@@ -44,7 +44,7 @@ class dbObject {
*
* @var modelPath
*/
private static $modelPath;
protected static $modelPath;
/**
* An array that holds object data
*
......@@ -59,6 +59,7 @@ class dbObject {
public $isNew = true;
/**
* Return type: 'Array' to return results as array, 'Object' as object
* 'Json' as json string
*
* @var string
*/
......@@ -170,6 +171,16 @@ class dbObject {
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
......@@ -299,6 +310,8 @@ class dbObject {
$this->processArrays ($results);
$this->data = $results;
$this->processWith ($results);
if ($this->returnType == 'Json')
return json_encode ($results);
if ($this->returnType == 'Array')
return $results;
......@@ -334,6 +347,9 @@ class dbObject {
if ($this->returnType == 'Object')
return $objects;
if ($this->returnType == 'Json')
return json_encode ($results);
return $results;
}
......
......@@ -128,7 +128,6 @@ if ($db->update ('users', $data))
else
echo 'update failed: ' . $db->getLastError();
```
### Select Query
After any select/get function calls amount or returned rows
is stored in $count variable
......@@ -165,7 +164,18 @@ or select one column value or function result
$count = $db->getValue ("users", "count(*)");
echo "{$count} users found";
```
### Defining a return type
MysqliDb can return result in 3 different formats: Array of Array, Array of Objects and a Json string. To select a return type use ArrayBuilder(), ObjectBuilder() and JsonBuilder() methods. Note that ArrayBuilder() is a default return type
```php
// Array return type
$= $db->getOne("users");
echo $u['login'];
// Object return type
$u = $db->ObjectBuilder()->getOne("users");
echo $u->login;
// Json return type
$json = $db->JsonBuilder()->getOne("users");
```
### Delete Query
```php
$db->where('id', 1);
......
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