diff options
Diffstat (limited to 'src/application/mystic/forum/Database.php')
-rw-r--r-- | src/application/mystic/forum/Database.php | 23 |
1 files changed, 18 insertions, 5 deletions
diff --git a/src/application/mystic/forum/Database.php b/src/application/mystic/forum/Database.php index 7c9ac7a..6d633fa 100644 --- a/src/application/mystic/forum/Database.php +++ b/src/application/mystic/forum/Database.php @@ -333,15 +333,28 @@ class Database { return true; } - public function fetchWhere(Entity &$entity, string $columnName): bool { + public function fetchWhere(Entity &$entity, string|array $columnNames): bool { $entityClassName = get_class($entity); $tableName = self::getTableName($entityClassName); $reflClass = new ReflectionClass($entityClassName); $cols = self::getColumns($reflClass); - if (!isset($cols[$columnName])) - throw new \RuntimeException("Column $columnName does not exist!"); - $query = "SELECT * FROM $tableName WHERE $columnName = \$1 LIMIT 1;"; - $result = \pg_query_params($this->connection, $query, [ $entity->{$cols[$columnName]["propertyName"]} ]); + if (!is_array($columnNames)) { + $columnNames = [ $columnNames ]; + } + + $whereClause = []; + $columnValues = []; + $count = 0; + foreach ($columnNames as $columnName) { + ++$count; + if (!isset($cols[$columnName])) + throw new \RuntimeException("Column $columnName does not exist!"); + $whereClause []= "$columnName = \$$count"; + $columnValues []= self::stringifyValue($entity->{$cols[$columnName]["propertyName"]}, $cols[$columnName]["columnType"]); + } + $whereClause = implode(" AND ", $whereClause); + $query = "SELECT * FROM $tableName WHERE $whereClause LIMIT 1;"; + $result = \pg_query_params($this->connection, $query, $columnValues); if ($result === false) throw new \RuntimeException("Fetch failed: " . \pg_last_error($this->connection)); $row = \pg_fetch_assoc($result); |