Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: catfan/Medoo
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: sajjad-rezaei/Medoo
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Can’t automatically merge. Don’t worry, you can still create the pull request.
  • 2 commits
  • 1 file changed
  • 1 contributor

Commits on Dec 11, 2021

  1. add return object type to select and get function

    Sajjad Rezaei authored and Sajjad Rezaei committed Dec 11, 2021
    Copy the full SHA
    5b38958 View commit details
  2. change of comment of return type

    Sajjad Rezaei authored and Sajjad Rezaei committed Dec 11, 2021
    Copy the full SHA
    b0c99f5 View commit details
Showing with 102 additions and 25 deletions.
  1. +102 −25 src/Medoo.php
127 changes: 102 additions & 25 deletions src/Medoo.php
Original file line number Diff line number Diff line change
@@ -76,6 +76,19 @@ class Medoo
*/
public $type;

/**
* The type of select to be return.
*
* @var int
*/
protected $returnType = PDO::FETCH_ASSOC;
/**
* Define whether select return type is object or not
*
* @var bool
*/
private $isReturnObject = false;

/**
* Table prefix.
*
@@ -471,6 +484,20 @@ public function __construct(array $options)
}
}

public function setReturnType($type){

switch($type){
case PDO::FETCH_OBJ:
$this->isReturnObject = true;
$this->returnType = PDO::FETCH_OBJ;
break;
case PDO::FETCH_ASSOC:
$this->isReturnObject = false;
$this->returnType = PDO::FETCH_ASSOC;
break;
}

}
/**
* Generate a new map key for placeholder.
*
@@ -1359,20 +1386,24 @@ protected function columnMap($columns, array &$stack, bool $root): array
* @return void
*/
protected function dataMap(
array $data,
$data,
array $columns,
array $columnMap,
array &$stack,
&$stack,
bool $root,
array &$result = null
): void {

if ($root) {
$columnsKey = array_keys($columns);

if (count($columnsKey) === 1 && is_array($columns[$columnsKey[0]])) {

$indexKey = array_keys($columns)[0];
$dataKey = preg_replace("/^(?![_\d])[\p{N}\p{L}\-_]+\./u", "", $indexKey);
$currentStack = [];
if($this->isReturnObject)
$currentStack = (object)[];
else
$currentStack = [];

foreach ($data as $item) {
$this->dataMap($data, $columns[$indexKey], $columnMap, $currentStack, false, $result);
@@ -1381,13 +1412,19 @@ protected function dataMap(
if (isset($result)) {
$result[$index] = $currentStack;
} else {
$stack[$index] = $currentStack;
if($this->isReturnObject)
$stack->$index = $currentStack;
else
$stack[$index] = $currentStack;
}
}
} else {
$currentStack = [];
if($this->isReturnObject)
$currentStack = (object)[];
else
$currentStack = [];
$this->dataMap($data, $columns, $columnMap, $currentStack, false, $result);

if (isset($result)) {
$result[] = $currentStack;
} else {
@@ -1400,56 +1437,93 @@ protected function dataMap(

foreach ($columns as $key => $value) {
$isRaw = $this->isRaw($value);

if (is_int($key) || $isRaw) {
$map = $columnMap[$isRaw ? $key : $value];

$columnKey = $map[0];
$item = $data[$columnKey];
//check if its object
if($this->isReturnObject)
$item = $data->$columnKey;
else
$item = $data[$columnKey];


if (isset($map[1])) {
if ($isRaw && in_array($map[1], ['Object', 'JSON'])) {
continue;
}

if (is_null($item)) {
$stack[$columnKey] = null;
if($this->isReturnObject)
$stack->$columnKey = null;
else
$stack[$columnKey] = null;
continue;
}

switch ($map[1]) {

case 'Number':
$stack[$columnKey] = (float) $item;
if($this->isReturnObject)
$stack->$columnKey = (float) $item;
else
$stack[$columnKey] = (float) $item;
break;

case 'Int':
$stack[$columnKey] = (int) $item;
if($this->isReturnObject)
$stack->$columnKey = (int) $item;
else
$stack[$columnKey] = (int) $item;
break;

case 'Bool':
$stack[$columnKey] = (bool) $item;
if($this->isReturnObject)
$stack->$columnKey = (bool) $item;
else
$stack[$columnKey] = (bool) $item;
break;

case 'Object':
$stack[$columnKey] = unserialize($item);
if($this->isReturnObject)
$stack->$columnKey = unserialize($item);
else
$stack[$columnKey] = unserialize($item);
break;

case 'JSON':
$stack[$columnKey] = json_decode($item, true);
if($this->isReturnObject)
$stack->$columnKey = json_decode($item, true);
else
$stack[$columnKey] = json_decode($item, true);
break;

case 'String':
$stack[$columnKey] = $item;
if($this->isReturnObject)
$stack->$columnKey = $item;
else
$stack[$columnKey] = $item;

break;
}
} else {
$stack[$columnKey] = $item;

if($this->isReturnObject)
$stack->$columnKey = $item;
else
$stack[$columnKey] = $item;
}
} else {
$currentStack = [];

if($this->isReturnObject)
$currentStack = (object)[];
else
$currentStack = [];
$this->dataMap($data, $value, $columnMap, $currentStack, false, $result);

$stack[$key] = $currentStack;
if($this->isReturnObject)
$stack->$key = $currentStack;
else
$stack[$key] = $currentStack;
}
}
}
@@ -1548,18 +1622,21 @@ public function select(string $table, $join, $columns = null, $where = null): ?a
// @codeCoverageIgnoreStart
if ($columns === '*') {
if (isset($callback)) {
while ($data = $statement->fetch(PDO::FETCH_ASSOC)) {
while ($data = $statement->fetch($this->returnType)) {
$callback($data);
}

return null;
}

return $statement->fetchAll(PDO::FETCH_ASSOC);
return $statement->fetchAll($this->returnType);
}

while ($data = $statement->fetch(PDO::FETCH_ASSOC)) {
$currentStack = [];
while ($data = $statement->fetch($this->returnType)) {
if($this->isReturnObject)
$currentStack = (object)[];
else
$currentStack = [];

if (isset($callback)) {
$this->dataMap($data, $columns, $columnMap, $currentStack, true);
@@ -1835,7 +1912,7 @@ public function get(string $table, $join = null, $columns = null, $where = null)
}

// @codeCoverageIgnoreStart
$data = $query->fetchAll(PDO::FETCH_ASSOC);
$data = $query->fetchAll($this->returnType);

if (isset($data[0])) {
if ($column === '*') {