diff options
author | Jonas Kohl | 2024-09-15 19:55:12 +0200 |
---|---|---|
committer | Jonas Kohl | 2024-09-15 19:55:12 +0200 |
commit | cb9b87997993702131ca24d4d0e1fd45ef64805c (patch) | |
tree | 3ba1725028665f90b546703c461394b577b3e596 /src/application/mystic/forum | |
parent | cc97f36b8c9a9522636d5b50fbcd2f52de06a01a (diff) |
Begun i18n work & more
Diffstat (limited to 'src/application/mystic/forum')
-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); |