Skip to content

Commit 59cb2d4

Browse files
committed
mysqlidb: Added ArrayBuilder,ObjectBuilder and JsonBuilder to be consistent with dbObject
1 parent 6a3e247 commit 59cb2d4

File tree

3 files changed

+69
-9
lines changed

3 files changed

+69
-9
lines changed

MysqliDb.php

Lines changed: 56 additions & 7 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,11 +398,12 @@ 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
}
@@ -376,7 +417,7 @@ public function getOne($tableName, $columns = '*')
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"];
@@ -801,9 +842,14 @@ protected function _dynamicBindResults(mysqli_stmt $stmt)
801842
$this->totalCount = 0;
802843
$this->count = 0;
803844
while ($stmt->fetch()) {
804-
$x = array();
805-
foreach ($row as $key => $val) {
806-
$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;
807853
}
808854
$this->count++;
809855
array_push($results, $x);
@@ -817,6 +863,9 @@ protected function _dynamicBindResults(mysqli_stmt $stmt)
817863
$totalCount = $stmt->fetch_row();
818864
$this->totalCount = $totalCount[0];
819865
}
866+
if ($this->returnType == 'Json') {
867+
return json_encode ($results);
868+
}
820869

821870
return $results;
822871
}

dbObject.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*/

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)