From 5b38958f3eee539a4f581cb49da63968e2a1f0aa Mon Sep 17 00:00:00 2001 From: Sajjad Rezaei Date: Sun, 12 Dec 2021 00:54:37 +0330 Subject: [PATCH 1/2] add return object type to select and get function --- src/Medoo.php | 127 ++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 102 insertions(+), 25 deletions(-) diff --git a/src/Medoo.php b/src/Medoo.php index 4b2ff1da..c3cd2e60 100644 --- a/src/Medoo.php +++ b/src/Medoo.php @@ -76,6 +76,19 @@ class Medoo */ public $type; + /** + * The type of select to be return. + * + * @var string + */ + protected $returnType = PDO::FETCH_ASSOC; + /** + * The type of select to be return. + * + * @var string + */ + 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 === '*') { From b0c99f5baeff83722c674bd0ea36c33cc8293f05 Mon Sep 17 00:00:00 2001 From: Sajjad Rezaei Date: Sun, 12 Dec 2021 01:03:00 +0330 Subject: [PATCH 2/2] change of comment of return type --- src/Medoo.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Medoo.php b/src/Medoo.php index c3cd2e60..e74ccbf2 100644 --- a/src/Medoo.php +++ b/src/Medoo.php @@ -79,13 +79,13 @@ class Medoo /** * The type of select to be return. * - * @var string + * @var int */ protected $returnType = PDO::FETCH_ASSOC; /** - * The type of select to be return. + * Define whether select return type is object or not * - * @var string + * @var bool */ private $isReturnObject = false;