diff --git a/config/phpstan/phpstan.neon b/config/phpstan/phpstan.neon index 9afe8152..a780a522 100644 --- a/config/phpstan/phpstan.neon +++ b/config/phpstan/phpstan.neon @@ -40,6 +40,7 @@ parameters: -'#^Safe\\#' uncheckedExceptionClasses: - 'Exceptions\DatabaseQueryException' + - 'Exceptions\MultiSelectMethodNotFoundException' - 'PDOException' - 'TypeError' - 'ValueError' diff --git a/lib/Db.php b/lib/Db.php index bb7c1b39..f48de6f2 100644 --- a/lib/Db.php +++ b/lib/Db.php @@ -19,6 +19,25 @@ class Db{ return $GLOBALS['DbConnection']->Query($query, $args, $class); } + /** + * Execute a select query that returns a join against multiple tables. + * + * @template T + * + * @param string $sql The SQL query to execute. + * @param array $params An array of parameters to bind to the SQL statement. + * @param class-string $class The class to instantiate for each row, or `null` to return an array of rows. + * + * @return array An array of `$class`. + * + * @throws Exceptions\MultiSelectMethodNotFoundException If a class was specified but the class doesn't have a `FromMultiTableRow()` method. + * @throws Exceptions\DatabaseQueryException When an error occurs during execution of the query. + */ + public static function MultiTableSelect(string $sql, array $params, string $class): array{ + /** @throws Exceptions\DatabaseQueryException|Exceptions\MultiSelectMethodNotFoundException */ + return $GLOBALS['DbConnection']->MultiTableSelect($sql, $params, $class); + } + /** * Returns a single integer value for the first column database query result. * diff --git a/lib/Project.php b/lib/Project.php index 05a5fd87..839d5fd2 100644 --- a/lib/Project.php +++ b/lib/Project.php @@ -652,21 +652,21 @@ final class Project{ * @return array */ public static function GetAllByStatus(Enums\ProjectStatusType $status): array{ - return Db::Query('SELECT * from Projects inner join Ebooks using (EbookId) where Projects.Status = ? order by Title asc', [$status], Project::class); + return Db::MultiTableSelect('SELECT * from Projects inner join Ebooks using (EbookId) where Projects.Status = ? order by Title asc', [$status], Project::class); } /** * @return array */ public static function GetAllByManagerUserId(int $userId): array{ - return Db::Query('SELECT * from Projects inner join Ebooks using (EbookId) where ManagerUserId = ? and Status in (?, ?) order by Title asc', [$userId, Enums\ProjectStatusType::InProgress, Enums\ProjectStatusType::Stalled], Project::class); + return Db::MultiTableSelect('SELECT * from Projects inner join Ebooks using (EbookId) where ManagerUserId = ? and Status in (?, ?) order by Title asc', [$userId, Enums\ProjectStatusType::InProgress, Enums\ProjectStatusType::Stalled], Project::class); } /** * @return array */ public static function GetAllByReviewerUserId(int $userId): array{ - return Db::Query('SELECT * from Projects inner join Ebooks using (EbookId) where ReviewerUserId = ? and Status in (?, ?) order by Title asc', [$userId, Enums\ProjectStatusType::InProgress, Enums\ProjectStatusType::Stalled], Project::class); + return Db::MultiTableSelect('SELECT * from Projects inner join Ebooks using (EbookId) where ReviewerUserId = ? and Status in (?, ?) order by Title asc', [$userId, Enums\ProjectStatusType::InProgress, Enums\ProjectStatusType::Stalled], Project::class); } /** @@ -677,9 +677,8 @@ final class Project{ public static function FromMultiTableRow(array $row): Project{ $object = Project::FromRow($row['Projects']); - // The Action may be null if it's a Scribophile adjustment. In that case, don't initialize the Action object. if($row['Ebooks']->EbookId !== null){ - $row['Ebooks']->Ebookid = $object->EbookId; + $row['Ebooks']->EbookId = $object->EbookId; $object->Ebook = Ebook::FromRow($row['Ebooks']); } diff --git a/www/ebook-placeholders/post.php b/www/ebook-placeholders/post.php index 978a8e1a..7f9da5ca 100644 --- a/www/ebook-placeholders/post.php +++ b/www/ebook-placeholders/post.php @@ -82,7 +82,6 @@ try{ // DELETE an `EbookPlaceholder`. if($httpMethod == Enums\HttpMethod::Delete){ $ebook = Ebook::GetByIdentifier($identifier); - $exceptionRedirectUrl = $ebook->Url; $ebook->Delete();