Skip to content

Commit 1fe4359

Browse files
committed
Merge pull request #255 from avbdr/master
Bugfixes an features
2 parents e0abe5b + 59cb2d4 commit 1fe4359

File tree

4 files changed

+103
-16
lines changed

4 files changed

+103
-16
lines changed

MysqliDb.php

Lines changed: 67 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,14 @@ class MysqliDb
112112
*/
113113
protected $isSubQuery = false;
114114

115+
/**
116+
* Return type: 'Array' to return results as array, 'Object' as object
117+
* 'Json' as json string
118+
*
119+
* @var string
120+
*/
121+
public $returnType = 'Object';
122+
115123
/**
116124
* Variables for query execution tracing
117125
*
@@ -211,6 +219,38 @@ protected function reset()
211219
$this->_bindParams = array(''); // Create the empty 0 index
212220
$this->_query = null;
213221
$this->_queryOptions = array();
222+
$this->returnType = 'Array';
223+
}
224+
225+
/**
226+
* Helper function to create dbObject with Json return type
227+
*
228+
* @return dbObject
229+
*/
230+
public function JsonBuilder () {
231+
$this->returnType = 'Json';
232+
return $this;
233+
}
234+
235+
/**
236+
* Helper function to create dbObject with Array return type
237+
* Added for consistency as thats default output type
238+
*
239+
* @return dbObject
240+
*/
241+
public function ArrayBuilder () {
242+
$this->returnType = 'Array';
243+
return $this;
244+
}
245+
246+
/**
247+
* Helper function to create dbObject with Object return type.
248+
*
249+
* @return dbObject
250+
*/
251+
public function ObjectBuilder () {
252+
$this->returnType = 'Object';
253+
return $this;
214254
}
215255

216256
/**
@@ -358,25 +398,26 @@ public function getOne($tableName, $columns = '*')
358398
{
359399
$res = $this->get ($tableName, 1, $columns);
360400

361-
if (is_object($res))
401+
if ($res instanceof MysqliDb)
362402
return $res;
363-
364-
if (isset($res[0]))
403+
else if (is_array ($res) && isset ($res[0]))
365404
return $res[0];
405+
else if ($res)
406+
return $res;
366407

367408
return null;
368409
}
369410

370411
/**
371-
* A convenient SELECT * function to get one value.
412+
* A convenient SELECT COLUMN function to get a single column value from one row
372413
*
373414
* @param string $tableName The name of the database table to work with.
374415
*
375-
* @return array Contains the returned column from the select query.
416+
* @return string Contains the value of a returned column.
376417
*/
377418
public function getValue($tableName, $column)
378419
{
379-
$res = $this->get ($tableName, 1, "{$column} as retval");
420+
$res = $this->ArrayBuilder()->get ($tableName, 1, "{$column} as retval");
380421

381422
if (isset($res[0]["retval"]))
382423
return $res[0]["retval"];
@@ -769,6 +810,9 @@ protected function _dynamicBindResults(mysqli_stmt $stmt)
769810
{
770811
$parameters = array();
771812
$results = array();
813+
// See http://php.net/manual/en/mysqli-result.fetch-fields.php
814+
$mysqlLongType = 252;
815+
$shouldStoreResult = false;
772816

773817
$meta = $stmt->result_metadata();
774818

@@ -780,23 +824,32 @@ protected function _dynamicBindResults(mysqli_stmt $stmt)
780824

781825
$row = array();
782826
while ($field = $meta->fetch_field()) {
827+
if ($field->type == $mysqlLongType)
828+
$shouldStoreResult = true;
829+
783830
$row[$field->name] = null;
784831
$parameters[] = & $row[$field->name];
785832
}
786833

787-
// avoid out of memory bug in php 5.2 and 5.3
834+
// avoid out of memory bug in php 5.2 and 5.3. Mysqli allocates lot of memory for long*
835+
// and blob* types. So to avoid out of memory issues store_result is used
788836
// https://github.com/joshcam/PHP-MySQLi-Database-Class/pull/119
789-
if (version_compare (phpversion(), '5.4', '<'))
837+
if ($shouldStoreResult)
790838
$stmt->store_result();
791839

792840
call_user_func_array(array($stmt, 'bind_result'), $parameters);
793841

794842
$this->totalCount = 0;
795843
$this->count = 0;
796844
while ($stmt->fetch()) {
797-
$x = array();
798-
foreach ($row as $key => $val) {
799-
$x[$key] = $val;
845+
if ($this->returnType == 'Object') {
846+
$x = new stdClass ();
847+
foreach ($row as $key => $val)
848+
$x->$key = $val;
849+
} else {
850+
$x = array();
851+
foreach ($row as $key => $val)
852+
$x[$key] = $val;
800853
}
801854
$this->count++;
802855
array_push($results, $x);
@@ -810,6 +863,9 @@ protected function _dynamicBindResults(mysqli_stmt $stmt)
810863
$totalCount = $stmt->fetch_row();
811864
$this->totalCount = $totalCount[0];
812865
}
866+
if ($this->returnType == 'Json') {
867+
return json_encode ($results);
868+
}
813869

814870
return $results;
815871
}

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: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class dbObject {
4444
*
4545
* @var modelPath
4646
*/
47-
private static $modelPath;
47+
protected static $modelPath;
4848
/**
4949
* An array that holds object data
5050
*
@@ -59,6 +59,7 @@ class dbObject {
5959
public $isNew = true;
6060
/**
6161
* Return type: 'Array' to return results as array, 'Object' as object
62+
* 'Json' as json string
6263
*
6364
* @var string
6465
*/
@@ -170,6 +171,16 @@ public function __unset ($name) {
170171
unset ($this->data[$name]);
171172
}
172173

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

174185
/**
175186
* Helper function to create dbObject with Array return type
@@ -299,6 +310,8 @@ private function getOne ($fields = null) {
299310
$this->processArrays ($results);
300311
$this->data = $results;
301312
$this->processWith ($results);
313+
if ($this->returnType == 'Json')
314+
return json_encode ($results);
302315
if ($this->returnType == 'Array')
303316
return $results;
304317

@@ -334,6 +347,9 @@ private function get ($limit = null, $fields = null) {
334347
if ($this->returnType == 'Object')
335348
return $objects;
336349

350+
if ($this->returnType == 'Json')
351+
return json_encode ($results);
352+
337353
return $results;
338354
}
339355

readme.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ if ($db->update ('users', $data))
128128
else
129129
echo 'update failed: ' . $db->getLastError();
130130
```
131-
132131
### Select Query
133132
After any select/get function calls amount or returned rows
134133
is stored in $count variable
@@ -165,7 +164,18 @@ or select one column value or function result
165164
$count = $db->getValue ("users", "count(*)");
166165
echo "{$count} users found";
167166
```
168-
167+
### Defining a return type
168+
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
169+
```php
170+
// Array return type
171+
$= $db->getOne("users");
172+
echo $u['login'];
173+
// Object return type
174+
$u = $db->ObjectBuilder()->getOne("users");
175+
echo $u->login;
176+
// Json return type
177+
$json = $db->JsonBuilder()->getOne("users");
178+
```
169179
### Delete Query
170180
```php
171181
$db->where('id', 1);

0 commit comments

Comments
 (0)