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 ...@@ -112,6 +112,14 @@ class MysqliDb
*/ */
protected $isSubQuery = false; 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 * Variables for query execution tracing
* *
...@@ -211,6 +219,38 @@ class MysqliDb ...@@ -211,6 +219,38 @@ class MysqliDb
$this->_bindParams = array(''); // Create the empty 0 index $this->_bindParams = array(''); // Create the empty 0 index
$this->_query = null; $this->_query = null;
$this->_queryOptions = array(); $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 ...@@ -358,25 +398,26 @@ class MysqliDb
{ {
$res = $this->get ($tableName, 1, $columns); $res = $this->get ($tableName, 1, $columns);
if (is_object($res)) if ($res instanceof MysqliDb)
return $res; return $res;
else if (is_array ($res) && isset ($res[0]))
if (isset($res[0]))
return $res[0]; return $res[0];
else if ($res)
return $res;
return null; 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. * @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) 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"])) if (isset($res[0]["retval"]))
return $res[0]["retval"]; return $res[0]["retval"];
...@@ -769,6 +810,9 @@ class MysqliDb ...@@ -769,6 +810,9 @@ class MysqliDb
{ {
$parameters = array(); $parameters = array();
$results = array(); $results = array();
// See http://php.net/manual/en/mysqli-result.fetch-fields.php
$mysqlLongType = 252;
$shouldStoreResult = false;
$meta = $stmt->result_metadata(); $meta = $stmt->result_metadata();
...@@ -780,13 +824,17 @@ class MysqliDb ...@@ -780,13 +824,17 @@ class MysqliDb
$row = array(); $row = array();
while ($field = $meta->fetch_field()) { while ($field = $meta->fetch_field()) {
if ($field->type == $mysqlLongType)
$shouldStoreResult = true;
$row[$field->name] = null; $row[$field->name] = null;
$parameters[] = & $row[$field->name]; $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 // https://github.com/joshcam/PHP-MySQLi-Database-Class/pull/119
if (version_compare (phpversion(), '5.4', '<')) if ($shouldStoreResult)
$stmt->store_result(); $stmt->store_result();
call_user_func_array(array($stmt, 'bind_result'), $parameters); call_user_func_array(array($stmt, 'bind_result'), $parameters);
...@@ -794,9 +842,14 @@ class MysqliDb ...@@ -794,9 +842,14 @@ class MysqliDb
$this->totalCount = 0; $this->totalCount = 0;
$this->count = 0; $this->count = 0;
while ($stmt->fetch()) { while ($stmt->fetch()) {
$x = array(); if ($this->returnType == 'Object') {
foreach ($row as $key => $val) { $x = new stdClass ();
$x[$key] = $val; foreach ($row as $key => $val)
$x->$key = $val;
} else {
$x = array();
foreach ($row as $key => $val)
$x[$key] = $val;
} }
$this->count++; $this->count++;
array_push($results, $x); array_push($results, $x);
...@@ -810,6 +863,9 @@ class MysqliDb ...@@ -810,6 +863,9 @@ class MysqliDb
$totalCount = $stmt->fetch_row(); $totalCount = $stmt->fetch_row();
$this->totalCount = $totalCount[0]; $this->totalCount = $totalCount[0];
} }
if ($this->returnType == 'Json') {
return json_encode ($results);
}
return $results; return $results;
} }
......
...@@ -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.
......
...@@ -44,7 +44,7 @@ class dbObject { ...@@ -44,7 +44,7 @@ class dbObject {
* *
* @var modelPath * @var modelPath
*/ */
private static $modelPath; protected static $modelPath;
/** /**
* An array that holds object data * An array that holds object data
* *
...@@ -59,6 +59,7 @@ class dbObject { ...@@ -59,6 +59,7 @@ class dbObject {
public $isNew = true; public $isNew = true;
/** /**
* Return type: 'Array' to return results as array, 'Object' as object * Return type: 'Array' to return results as array, 'Object' as object
* 'Json' as json string
* *
* @var string * @var string
*/ */
...@@ -170,6 +171,16 @@ class dbObject { ...@@ -170,6 +171,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 +310,8 @@ class dbObject { ...@@ -299,6 +310,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 +347,9 @@ class dbObject { ...@@ -334,6 +347,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;
} }
......
...@@ -128,7 +128,6 @@ if ($db->update ('users', $data)) ...@@ -128,7 +128,6 @@ if ($db->update ('users', $data))
else else
echo 'update failed: ' . $db->getLastError(); echo 'update failed: ' . $db->getLastError();
``` ```
### Select Query ### Select Query
After any select/get function calls amount or returned rows After any select/get function calls amount or returned rows
is stored in $count variable is stored in $count variable
...@@ -165,7 +164,18 @@ or select one column value or function result ...@@ -165,7 +164,18 @@ or select one column value or function result
$count = $db->getValue ("users", "count(*)"); $count = $db->getValue ("users", "count(*)");
echo "{$count} users found"; 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 ### Delete Query
```php ```php
$db->where('id', 1); $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