From a4e277184e95c7aafa5234ec8859889b187738b3 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Thu, 14 Sep 2017 08:59:09 +0200 Subject: [PATCH] Fix grammar bugs --- DataStorage/Database/DataMapperAbstract.php | 9 +++-- DataStorage/Database/GrammarAbstract.php | 33 ++++++++++++++++++- .../Database/Query/Grammar/Grammar.php | 2 +- 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/DataStorage/Database/DataMapperAbstract.php b/DataStorage/Database/DataMapperAbstract.php index 0f059248c..6bab26dac 100644 --- a/DataStorage/Database/DataMapperAbstract.php +++ b/DataStorage/Database/DataMapperAbstract.php @@ -2239,11 +2239,14 @@ class DataMapperAbstract implements DataMapperInterface */ public static function getPrimaryKeyBy($refKey, string $ref) : array { - $query = self::getQuery(); - $query->select(static::$primaryField) + $query = new Builder(self::$db); + $query->prefix(self::$db->getPrefix()) + ->select(static::$table . '.' . static::$primaryField) ->from(static::$table) ->where(static::$table . '.' . $ref, '=', $refKey); + var_dump($query->toSql()); + $sth = self::$db->con->prepare($query->toSql()); $sth->execute(); @@ -2493,6 +2496,6 @@ class DataMapperAbstract implements DataMapperInterface } } - throw \Exception(); + throw new \Exception('Invalid member name'); } } diff --git a/DataStorage/Database/GrammarAbstract.php b/DataStorage/Database/GrammarAbstract.php index 1039e97ab..a221750b1 100644 --- a/DataStorage/Database/GrammarAbstract.php +++ b/DataStorage/Database/GrammarAbstract.php @@ -172,7 +172,7 @@ abstract class GrammarAbstract foreach ($elements as $key => $element) { if (is_string($element) && $element !== '*') { - if(strpos($element, '.')) { + if(strpos($element, '.') === false) { $prefix = ''; } @@ -191,6 +191,37 @@ abstract class GrammarAbstract return rtrim($expression, ', '); } + /** + * Expressionize elements. + * + * @param array $elements Elements + * @param string $prefix Prefix for table + * + * @return string + * + * @since 1.0.0 + */ + protected function expressionizeTable(array $elements, string $prefix = '') : string + { + $expression = ''; + + foreach ($elements as $key => $element) { + if (is_string($element) && $element !== '*') { + $expression .= $this->compileSystem($element, $prefix) . ', '; + } elseif (is_string($element) && $element === '*') { + $expression .= '*, '; + } elseif ($element instanceof \Closure) { + $expression .= $element() . ', '; + } elseif ($element instanceof BuilderAbstract) { + $expression .= $element->toSql() . ', '; + } else { + throw new \InvalidArgumentException(); + } + } + + return rtrim($expression, ', '); + } + /** * Compile system. * diff --git a/DataStorage/Database/Query/Grammar/Grammar.php b/DataStorage/Database/Query/Grammar/Grammar.php index a67b302b1..f3269307c 100644 --- a/DataStorage/Database/Query/Grammar/Grammar.php +++ b/DataStorage/Database/Query/Grammar/Grammar.php @@ -169,7 +169,7 @@ class Grammar extends GrammarAbstract */ protected function compileFrom(Builder $query, array $table) : string { - $expression = $this->expressionizeTableColumn($table, $query->getPrefix()); + $expression = $this->expressionizeTable($table, $query->getPrefix()); if ($expression === '') { return '';