From ee1422ff200608109c5067ad4f5cff637712cbcb Mon Sep 17 00:00:00 2001 From: Mat Fish Date: Mon, 29 Nov 2021 09:51:28 +0200 Subject: [PATCH] fix postgres query --- CHANGELOG.md | 6 +++++- src/services/EntryMetaQueryService.php | 24 ++++++++++++++++-------- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bbb8597..cf0db72 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Release Notes for Entry Meta -## 1.0.0-rc.1 +## 1.0.0-rc.2 - 2021-11-29 +### Fixed +- Fixed Postgres query + +## 1.0.0-rc.1 - 2021-11-28 ### Added - Initial release \ No newline at end of file diff --git a/src/services/EntryMetaQueryService.php b/src/services/EntryMetaQueryService.php index e9e6333..a7a47e4 100644 --- a/src/services/EntryMetaQueryService.php +++ b/src/services/EntryMetaQueryService.php @@ -23,7 +23,7 @@ public function findEntriesByMetadata(array $params) $first = true; - foreach ($params as $key=>$value) { + foreach ($params as $key => $value) { $method = $first ? 'where' : 'andWhere'; $q = $q->{$method}($this->_getCondition($key, $value)); $first = false; @@ -60,7 +60,7 @@ private function _transformValue($value) return $value ? 'true' : 'false'; } - if (is_string($value) || $this->dbDriver===self::POSTGRES) { + if (is_string($value) || $this->dbDriver === self::POSTGRES) { return "'{$value}'"; } @@ -69,14 +69,22 @@ private function _transformValue($value) private function _transformKey($key) { - if ($this->dbDriver===self::POSTGRES) { + if ($this->dbDriver === self::POSTGRES) { $keys = explode('.', $key); - $keys = array_map(function($key) { + $keys = array_map(function ($key) { return "'{$key}'"; - },$keys); - $nested = count($keys)>1; - $key = implode('->', $keys); - $key = ($nested ? '->':'->>') . $key; + }, $keys); + $nested = count($keys) > 1; + + if ($nested) { + $lastSegment = array_pop($keys); + $key = implode('->', $keys); + $key .= '->>' . $lastSegment; + $key = '->' . $key; + } else { + $key = $keys[0]; + $key = '->>' . $key; + } } return $key;