Skip to content

Commit 1e79753

Browse files
committed
Merge pull request #346 from avbdr/master
fixes
2 parents ec0feac + e4353be commit 1e79753

File tree

4 files changed

+78
-4
lines changed

4 files changed

+78
-4
lines changed

MysqliDb.php

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,12 @@ class MysqliDb
152152
*/
153153
protected $_lockInShareMode = false;
154154

155+
/**
156+
* Key field for Map()'ed result array
157+
* @var string
158+
*/
159+
protected $_mapKey = null;
160+
155161
/**
156162
* Variables for query execution tracing
157163
*
@@ -266,6 +272,7 @@ protected function reset()
266272
$this->_tableName = '';
267273
$this->_lastInsertId = null;
268274
$this->_updateColumns = null;
275+
$this->_mapKey = null;
269276
}
270277

271278
/**
@@ -999,7 +1006,10 @@ protected function _dynamicBindResults(mysqli_stmt $stmt)
9991006
}
10001007
}
10011008
$this->count++;
1002-
array_push ($results, $x);
1009+
if ($this->_mapKey)
1010+
$results[$row[$this->_mapKey]] = count ($row) > 2 ? $x : end ($x);
1011+
else
1012+
array_push ($results, $x);
10031013
}
10041014
if ($shouldStoreResult)
10051015
$stmt->free_result();
@@ -1095,7 +1105,15 @@ protected function _buildOnDuplicate($tableData)
10951105
if ($this->_lastInsertId)
10961106
$this->_query .= $this->_lastInsertId . "=LAST_INSERT_ID (".$this->_lastInsertId."), ";
10971107

1098-
$this->_buildDataPairs ($tableData, $this->_updateColumns, false);
1108+
foreach ($this->_updateColumns as $key => $val) {
1109+
// skip all params without a value
1110+
if (is_numeric ($key)) {
1111+
$this->_updateColumns[$val] = '';
1112+
unset ($this->_updateColumns[$key]);
1113+
} else
1114+
$tableData[$key] = $val;
1115+
}
1116+
$this->_buildDataPairs ($tableData, array_keys ($this->_updateColumns), false);
10991117
}
11001118
}
11011119

@@ -1521,4 +1539,16 @@ public function tableExists ($tables) {
15211539
$this->get ('information_schema.tables', $count);
15221540
return $this->count == $count;
15231541
}
1542+
1543+
/**
1544+
* Return result as an associative array with $idField field value used as a record key
1545+
*
1546+
* @param String $idField field name to use for a mapped element key
1547+
*
1548+
* @return Array Returns an array($k => $v) if get(.."param1, param2"), array ($k => array ($v, $v)) otherwise
1549+
*/
1550+
public function map ($idField) {
1551+
$this->_mapKey = $idField;
1552+
return $this;
1553+
}
15241554
} // END class

dbObject.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ public function __unset ($name) {
186186
*/
187187
private function JsonBuilder () {
188188
$this->returnType = 'Json';
189-
return $return;
189+
return $this;
190190
}
191191

192192
/**

readme.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,29 @@ foreach ($logins as $login)
198198
echo $login;
199199
```
200200

201+
### Result transformation / map
202+
Instead of getting an pure array of results its possible to get result in an associative array with a needed key. If only 2 fields to fetch will be set in get(),
203+
method will return result in array($k => $v) and array ($k => array ($v, $v)) in rest of the cases.
204+
205+
```php
206+
$user = $db->map ('login')->ObjectBuilder()->getOne ('users', 'login, id');
207+
Array
208+
(
209+
[user1] => 1
210+
)
211+
212+
$user = $db->map ('login')->ObjectBuilder()->getOne ('users', 'id,login,createdAt');
213+
Array
214+
(
215+
[user1] => stdClass Object
216+
(
217+
[id] => 1
218+
[login] => user1
219+
[createdAt] => 2015-10-22 22:27:53
220+
)
221+
222+
)
223+
```
201224

202225
### Defining a return type
203226
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

tests/mysqliDbTests.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,28 @@ function createTable ($name, $data) {
386386
echo "error in totalCount";
387387
exit;
388388
}
389+
390+
$result = $db->map ('id')->ArrayBuilder()->getOne ('users', 'id,login');
391+
if (key ($result) != 1 && $result[1] != 'user1') {
392+
echo 'map string=string failed';
393+
exit;
394+
}
395+
$result = $db->map ('id')->ArrayBuilder()->getOne ('users', 'id,login,createdAt');
396+
if (key ($result) != 1 && !is_array ($result[1])) {
397+
echo 'map string=array failed';
398+
exit;
399+
}
400+
$result = $db->map ('id')->ObjectBuilder()->getOne ('users', 'id,login');
401+
if (key ($result) != 1 && $result[1] != 'user1') {
402+
echo 'map object string=string failed';
403+
exit;
404+
}
405+
$result = $db->map ('id')->ObjectBuilder()->getOne ('users', 'id,login,createdAt');
406+
if (key ($result) != 1 && !is_object ($result[1])) {
407+
echo 'map string=object failed';
408+
exit;
409+
}
410+
389411
///
390412
//TODO: insert test
391413
$db->delete("users");
@@ -396,7 +418,6 @@ function createTable ($name, $data) {
396418
}
397419
$db->delete("products");
398420

399-
400421
//print_r($db->rawQuery("CALL simpleproc(?)",Array("test")));
401422

402423
print_r ($db->trace);

0 commit comments

Comments
 (0)