From bc7fe194c7a227ed4bc54e2fd58ec2de20b5f0a6 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Sat, 23 Jan 2021 12:29:02 +0100 Subject: [PATCH] better sql error handling and new request data get function --- DataStorage/Database/DataMapperAbstract.php | 9 +++++--- DataStorage/Database/Query/Builder.php | 16 ++++++++------ Message/RequestAbstract.php | 24 ++++++++++++++++++--- 3 files changed, 37 insertions(+), 12 deletions(-) diff --git a/DataStorage/Database/DataMapperAbstract.php b/DataStorage/Database/DataMapperAbstract.php index 53245403f..b916bbbe8 100644 --- a/DataStorage/Database/DataMapperAbstract.php +++ b/DataStorage/Database/DataMapperAbstract.php @@ -601,7 +601,7 @@ class DataMapperAbstract implements DataMapperInterface } } catch (\Throwable $t) { \var_dump($t->getMessage()); - \var_dump($query->toSql()); + \var_dump($a = $query->toSql()); return -1; } @@ -2271,7 +2271,10 @@ class DataMapperAbstract implements DataMapperInterface static::$ownsOne[$def['internal']]['mapper']::fillRelations($value, self::$relations, $depth - 1); } - $refProp->setValue($obj, $value); + if (!empty($value)) { + // todo: find better solution. this was because of a bug with the sales billing list query depth = 4. The address was set (from the client, referral or creator) but then somehow there was a second address element which was all null and null cannot be asigned to a string variable (e.g. country). The problem with this solution is that if the model expects an initialization (e.g. at lest set the elements to null, '', 0 etc.) this is now not done. + $refProp->setValue($obj, $value); + } } elseif (isset(static::$belongsTo[$def['internal']])) { $default = null; if ($depth - 1 < 1 && $refProp->isInitialized($obj)) { @@ -2556,7 +2559,7 @@ class DataMapperAbstract implements DataMapperInterface int $depth = 3 ) : array { - $query = self::getQuery(); + $query = self::getQuery(depth: $depth); $query->where(static::$table . '_' . $depth . '.' . ($column !== null ? self::getColumnByMember($column) : static::$primaryField), '>', $pivot) ->orderBy(static::$table . '_' . $depth . '.' . ($column !== null ? self::getColumnByMember($column) : static::$primaryField), $order) ->limit($limit); diff --git a/DataStorage/Database/Query/Builder.php b/DataStorage/Database/Query/Builder.php index 3cb7b7cee..150775cb4 100644 --- a/DataStorage/Database/Query/Builder.php +++ b/DataStorage/Database/Query/Builder.php @@ -1401,16 +1401,20 @@ class Builder extends BuilderAbstract */ public function execute() : mixed { - $sth = $this->connection->con->prepare($this->toSql()); + try { + $sth = $this->connection->con->prepare($this->toSql()); - foreach ($this->binds as $key => $bind) { - $type = self::getBindParamType($bind); + foreach ($this->binds as $key => $bind) { + $type = self::getBindParamType($bind); - $sth->bindParam($key, $bind, $type); + $sth->bindParam($key, $bind, $type); + } + + $sth->execute(); + } catch (\Throwable $t) { + var_dump($this->toSql()); } - $sth->execute(); - return $sth; } diff --git a/Message/RequestAbstract.php b/Message/RequestAbstract.php index 831ded907..c76f7788e 100644 --- a/Message/RequestAbstract.php +++ b/Message/RequestAbstract.php @@ -77,13 +77,14 @@ abstract class RequestAbstract implements MessageInterface /** * Get data. * - * @param string $key Data key + * @param string $key Data key + * @param string $type Return type * * @return mixed * * @since 1.0.0 */ - public function getData(string $key = null) : mixed + public function getData(string $key = null, string $type = null) : mixed { if ($key === null) { return $this->data; @@ -91,7 +92,24 @@ abstract class RequestAbstract implements MessageInterface $key = \mb_strtolower($key); - return $this->data[$key] ?? null; + if ($type === null) { + return $this->data[$key] ?? null; + } + + if (!isset($this->data[$key])) { + return null; + } + + switch ($type) { + case 'int': + return (int) $this->data[$key]; + case 'string': + return (string) $this->data[$key]; + case 'float': + return (float) $this->data[$key]; + default: + return $this->data[$key]; + } } /**