diff options
author | Thomas Lange <code@nerdmind.de> | 2021-06-22 01:18:02 +0200 |
---|---|---|
committer | Thomas Lange <code@nerdmind.de> | 2021-06-22 01:46:42 +0200 |
commit | 8cd1105b111b89106f24c5b50795afb5ff28a935 (patch) | |
tree | c789faac9de4d83cb409c45779e76be8938a4d66 /core/include/page/list.php | |
parent | 7937df540b7d70b2bb87797442d0a0a0df197133 (diff) | |
download | blog-8cd1105b111b89106f24c5b50795afb5ff28a935.tar.gz blog-8cd1105b111b89106f24c5b50795afb5ff28a935.tar.xz blog-8cd1105b111b89106f24c5b50795afb5ff28a935.zip |
Implement new Repository and Entity classes
This commit adds new Repository and Entity classes which are better
abstracted from the rest of the application. They dont know anymore
about configuration options or how to parse to HTML because this is
not the job for the ORM but for other parts of the application.
The previous commits were a preparation for this big change.
An entity now represents just a single record from a specific table
of the database – nothing more. The repositories job is it to fetch
or update records of the database and instantiate the entities.
Another problem that was solved is the high amount of database queries
that was needed before. For example, on the blogs home page first were
all 10 latest post IDs fetched from the database and then another query
was executed with "WHERE id = :id" for *each* single post?! ...
This problem is solved with the new repository classes; they now use a
single query to fetch and build the entities of the 10 latest posts.
This change also solves the problem with database queries spread across
the application and limits the exzessive use of try/catch blocks which
were used before. The new classes make the whole code much cleaner. :)
Diffstat (limited to 'core/include/page/list.php')
-rw-r--r-- | core/include/page/list.php | 35 |
1 files changed, 20 insertions, 15 deletions
diff --git a/core/include/page/list.php b/core/include/page/list.php index 7618009..f7541fa 100644 --- a/core/include/page/list.php +++ b/core/include/page/list.php @@ -2,16 +2,21 @@ #=============================================================================== # Get instances #=============================================================================== -$Database = Application::getDatabase(); $Language = Application::getLanguage(); #=============================================================================== +# Get repositories +#=============================================================================== +$PageRepository = Application::getRepository('Page'); +$UserRepository = Application::getRepository('User'); + +#=============================================================================== # Pagination #=============================================================================== $site_size = Application::get('PAGE.LIST_SIZE'); $site_sort = Application::get('PAGE.LIST_SORT'); -$count = $Database->query(sprintf('SELECT COUNT(id) FROM %s', Page\Attribute::TABLE))->fetchColumn(); +$count = $PageRepository->getCount(); $lastSite = ceil($count / $site_size); $currentSite = HTTP::GET('site') ?? 1; @@ -24,23 +29,23 @@ if($currentSite < 1 OR ($currentSite > $lastSite AND $lastSite > 0)) { #=============================================================================== # Single redirect #=============================================================================== -if(Application::get('PAGE.SINGLE_REDIRECT') === TRUE AND $count === '1') { - $Statement = $Database->query(sprintf('SELECT id FROM %s LIMIT 1', Page\Attribute::TABLE)); - $Page = Page\Factory::build($Statement->fetchColumn()); +if(Application::get('PAGE.SINGLE_REDIRECT') === TRUE AND $count === 1) { + $Page = $PageRepository->getLast(); HTTP::redirect(Application::getEntityURL($Page)); } -$execSQL = "SELECT id FROM %s ORDER BY {$site_sort} LIMIT ".(($currentSite-1) * $site_size).", {$site_size}"; -$pageIDs = $Database->query(sprintf($execSQL, Page\Attribute::TABLE))->fetchAll($Database::FETCH_COLUMN); +#=============================================================================== +# Get paginated page list +#=============================================================================== +$pages = $PageRepository->getPaginated( + $site_sort, + $site_size, + ($currentSite-1) * $site_size +); -foreach($pageIDs as $pageID) { - try { - $Page = Page\Factory::build($pageID); - $User = User\Factory::build($Page->get('user')); - $templates[] = generatePageItemTemplate($Page, $User); - } - catch(Page\Exception $Exception){} - catch(User\Exception $Exception){} +foreach($pages as $Page) { + $User = $UserRepository->find($Page->get('user')); + $templates[] = generatePageItemTemplate($Page, $User); } #=============================================================================== |