Skip to content

Commit 2be9dac

Browse files
committed
new query option MYSQLI_NESTJOIN to join fields by joined table name
1 parent dedbb0b commit 2be9dac

File tree

1 file changed

+31
-7
lines changed

1 file changed

+31
-7
lines changed

MysqliDb.php

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@ class MysqliDb
120120
*/
121121
public $returnType = 'Array';
122122

123+
/**
124+
* Should join() results be nested by table
125+
* @var boolean
126+
*/
127+
protected $_nestJoin = false;
128+
private $_tableName = '';
123129
/**
124130
* Variables for query execution tracing
125131
*
@@ -220,6 +226,8 @@ protected function reset()
220226
$this->_query = null;
221227
$this->_queryOptions = array();
222228
$this->returnType = 'Array';
229+
$this->_nestJoin = false;
230+
$this->_tableName = '';
223231
}
224232

225233
/**
@@ -332,7 +340,7 @@ public function query($query, $numRows = null)
332340
public function setQueryOption ($options) {
333341
$allowedOptions = Array ('ALL','DISTINCT','DISTINCTROW','HIGH_PRIORITY','STRAIGHT_JOIN','SQL_SMALL_RESULT',
334342
'SQL_BIG_RESULT','SQL_BUFFER_RESULT','SQL_CACHE','SQL_NO_CACHE', 'SQL_CALC_FOUND_ROWS',
335-
'LOW_PRIORITY','IGNORE','QUICK');
343+
'LOW_PRIORITY','IGNORE','QUICK', 'MYSQLI_NESTJOIN');
336344
if (!is_array ($options))
337345
$options = Array ($options);
338346

@@ -341,7 +349,10 @@ public function setQueryOption ($options) {
341349
if (!in_array ($option, $allowedOptions))
342350
die ('Wrong query option: '.$option);
343351

344-
$this->_queryOptions[] = $option;
352+
if ($option == 'MYSQLI_NESTJOIN')
353+
$this->_nestJoin = true;
354+
else
355+
$this->_queryOptions[] = $option;
345356
}
346357

347358
return $this;
@@ -372,8 +383,9 @@ public function get($tableName, $numRows = null, $columns = '*')
372383
$columns = '*';
373384

374385
$column = is_array($columns) ? implode(', ', $columns) : $columns;
386+
$this->_tableName = self::$prefix . $tableName;
375387
$this->_query = 'SELECT ' . implode(' ', $this->_queryOptions) . ' ' .
376-
$column . " FROM " .self::$prefix . $tableName;
388+
$column . " FROM " . $this->_tableName;
377389
$stmt = $this->_buildQuery($numRows);
378390

379391
if ($this->isSubQuery)
@@ -827,8 +839,14 @@ protected function _dynamicBindResults(mysqli_stmt $stmt)
827839
if ($field->type == $mysqlLongType)
828840
$shouldStoreResult = true;
829841

830-
$row[$field->name] = null;
831-
$parameters[] = & $row[$field->name];
842+
if ($this->_nestJoin && $field->table != $this->_tableName) {
843+
$field->table = substr ($field->table, strlen (self::$prefix));
844+
$row[$field->table][$field->name] = null;
845+
$parameters[] = & $row[$field->table][$field->name];
846+
} else {
847+
$row[$field->name] = null;
848+
$parameters[] = & $row[$field->name];
849+
}
832850
}
833851

834852
// avoid out of memory bug in php 5.2 and 5.3. Mysqli allocates lot of memory for long*
@@ -844,8 +862,14 @@ protected function _dynamicBindResults(mysqli_stmt $stmt)
844862
while ($stmt->fetch()) {
845863
if ($this->returnType == 'Object') {
846864
$x = new stdClass ();
847-
foreach ($row as $key => $val)
848-
$x->$key = $val;
865+
foreach ($row as $key => $val) {
866+
if (is_array ($val)) {
867+
$x->$key = new stdClass ();
868+
foreach ($val as $k => $v)
869+
$x->$key->$k = $v;
870+
} else
871+
$x->$key = $val;
872+
}
849873
} else {
850874
$x = array();
851875
foreach ($row as $key => $val)

0 commit comments

Comments
 (0)