diff --git a/DataStorage/Database/DataMapperAbstract.php b/DataStorage/Database/DataMapperAbstract.php index 6067d8938..967128842 100644 --- a/DataStorage/Database/DataMapperAbstract.php +++ b/DataStorage/Database/DataMapperAbstract.php @@ -335,22 +335,31 @@ class DataMapperAbstract implements DataMapperInterface $where1 = new Where(self::$db); $where2 = new Where(self::$db); + $hasConditionals = false; foreach (self::$conditionals as $condKey => $condValue) { if (($column = self::getColumnByMember($condKey)) !== null) { $where1->andWhere(static::$table . '_' . $searchDepth . '.' . $column, '=', $condValue); + $hasConditionals = true; } } + $hasAutocompletes = false; foreach (static::$columns as $col) { if (isset($col['autocomplete']) && $col['autocomplete']) { $where2->where(static::$table . '_' . $searchDepth . '.' . $col['name'], 'LIKE', '%' . $search . '%', 'OR'); + $hasAutocompletes = true; } } - $query->andWhere($where1); - $query->andWhere($where2); + if ($hasConditionals) { + $query->andWhere($where1); + } - if ($searchDepth > 2) { + if ($hasAutocompletes) { + $query->andWhere($where2); + } + + if ($searchDepth > 1) { foreach (static::$ownsOne as $one) { $one['mapper']::findQuery($search, $searchDepth - 1, $query); } diff --git a/DataStorage/Database/Query/Builder.php b/DataStorage/Database/Query/Builder.php index 97f3f97fd..9bbf5e3d6 100644 --- a/DataStorage/Database/Query/Builder.php +++ b/DataStorage/Database/Query/Builder.php @@ -1471,6 +1471,8 @@ class Builder extends BuilderAbstract return $column->getColumn(); } elseif ($column instanceof \Serializable) { return $column->serialize(); + } elseif ($column instanceof self) { + return \md5($column->toSql()); } throw new \Exception(); diff --git a/DataStorage/Database/Query/Grammar/Grammar.php b/DataStorage/Database/Query/Grammar/Grammar.php index 2aa28c740..a246998d1 100644 --- a/DataStorage/Database/Query/Grammar/Grammar.php +++ b/DataStorage/Database/Query/Grammar/Grammar.php @@ -253,15 +253,16 @@ class Grammar extends GrammarAbstract $expression .= $this->compileSystem($element['column']); } elseif ($element['column'] instanceof \Closure) { $expression .= $element['column'](); + } elseif ($element['column'] instanceof Where) { + $where = \rtrim($this->compileWhereQuery($element['column']), ';'); + $expression .= '(' . (\stripos($where, 'WHERE ') === 0 ? \substr($where, 6) : $where) . ')'; } elseif ($element['column'] instanceof Builder) { $expression .= '(' . \rtrim($element['column']->toSql(), ';') . ')'; - } elseif ($element['column'] instanceof Where) { - $expression .= '(' . \rtrim($this->compileWhereQuery($element['column']), ';') . ')'; } if (isset($element['value'])) { $expression .= ' ' . \strtoupper($element['operator']) . ' ' . $this->compileValue($query, $element['value']); - } else { + } elseif (isset($element['value']) && !($element['column'] instanceof Where)) { $operator = $element['operator'] === '=' ? 'IS' : 'IS NOT'; $expression .= ' ' . $operator . ' ' . $this->compileValue($query, $element['value']); } diff --git a/DataStorage/Database/Query/Where.php b/DataStorage/Database/Query/Where.php index efd82516a..5cc163e22 100644 --- a/DataStorage/Database/Query/Where.php +++ b/DataStorage/Database/Query/Where.php @@ -14,6 +14,8 @@ declare(strict_types=1); namespace phpOMS\DataStorage\Database\Query; +use phpOMS\DataStorage\Database\Connection\ConnectionAbstract; + /** * Database query builder. * @@ -24,4 +26,16 @@ namespace phpOMS\DataStorage\Database\Query; */ class Where extends Builder { + /** + * Constructor. + * + * @param ConnectionAbstract $connection Database connection + * + * @since 1.0.0 + */ + public function __construct(ConnectionAbstract $connection) + { + parent::__construct($connection); + $this->type = QueryType::SELECT; + } }