diff options
author | Thomas Lange <code@nerdmind.de> | 2018-04-01 19:16:13 +0200 |
---|---|---|
committer | Thomas Lange <code@nerdmind.de> | 2018-04-01 19:16:13 +0200 |
commit | 91d8a28c664afa5378735bcd0efe068dd74d589f (patch) | |
tree | ae3d4809959dffbc9ea6118ecba76addd8b0abe8 /admin | |
parent | ea29b80ec2942f7e0390e3bb98c37ac6c4c5686d (diff) | |
download | blog-91d8a28c664afa5378735bcd0efe068dd74d589f.tar.gz blog-91d8a28c664afa5378735bcd0efe068dd74d589f.tar.xz blog-91d8a28c664afa5378735bcd0efe068dd74d589f.zip |
Use method "buildByAttribute" to create Item instances for item listing
This commit significantly reduces the number of database queries required to display a list of pages, posts or users. This could be achieved by using "SELECT * FROM […]" in combination with the new implemented factory method "buildByAttribute".
Previously, the first database query returned an array of unique item IDs that were then passed to the factory method "build" within the "foreach" loop which caused the application to make an additional database query like "SELECT * FROM […] WHERE id = {current_id}" for every single item ID to get it's payload data. Since this commit, this additional query for every item is not necessary anymore.
Diffstat (limited to 'admin')
-rw-r--r-- | admin/page/index.php | 10 | ||||
-rw-r--r-- | admin/post/index.php | 10 | ||||
-rw-r--r-- | admin/user/index.php | 10 |
3 files changed, 15 insertions, 15 deletions
diff --git a/admin/page/index.php b/admin/page/index.php index c14bf13..caff257 100644 --- a/admin/page/index.php +++ b/admin/page/index.php @@ -26,18 +26,18 @@ if($currentSite < 1 OR ($currentSite > $lastSite AND $lastSite > 0)) { } #=============================================================================== -# Fetch page IDs from database +# Fetch items from database #=============================================================================== -$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); +$execSQL = "SELECT * FROM %s ORDER BY {$site_sort} LIMIT ".(($currentSite-1) * $site_size).", {$site_size}"; +$Statement = $Database->query(sprintf($execSQL, Page\Attribute::TABLE)); #=============================================================================== # TRY: Template\Exception #=============================================================================== try { - foreach($pageIDs as $pageID) { + while($Attribute = $Statement->fetchObject('Page\Attribute')) { try { - $Page = Page\Factory::build($pageID); + $Page = Page\Factory::buildByAttribute($Attribute); $User = User\Factory::build($Page->attr('user')); $ItemTemplate = generatePageItemTemplate($Page, $User); diff --git a/admin/post/index.php b/admin/post/index.php index 57b19e9..7e492ba 100644 --- a/admin/post/index.php +++ b/admin/post/index.php @@ -26,18 +26,18 @@ if($currentSite < 1 OR ($currentSite > $lastSite AND $lastSite > 0)) { } #=============================================================================== -# Fetch post IDs from database +# Fetch items from database #=============================================================================== -$execSQL = "SELECT id FROM %s ORDER BY {$site_sort} LIMIT ".(($currentSite-1) * $site_size).", {$site_size}"; -$postIDs = $Database->query(sprintf($execSQL, Post\Attribute::TABLE))->fetchAll($Database::FETCH_COLUMN); +$execSQL = "SELECT * FROM %s ORDER BY {$site_sort} LIMIT ".(($currentSite-1) * $site_size).", {$site_size}"; +$Statement = $Database->query(sprintf($execSQL, Post\Attribute::TABLE)); #=============================================================================== # TRY: Template\Exception #=============================================================================== try { - foreach($postIDs as $postID) { + while($Attribute = $Statement->fetchObject('Post\Attribute')) { try { - $Post = Post\Factory::build($postID); + $Post = Post\Factory::buildByAttribute($Attribute); $User = User\Factory::build($Post->attr('user')); $ItemTemplate = generatePostItemTemplate($Post, $User); diff --git a/admin/user/index.php b/admin/user/index.php index b474fc8..dc5a4e7 100644 --- a/admin/user/index.php +++ b/admin/user/index.php @@ -26,18 +26,18 @@ if($currentSite < 1 OR ($currentSite > $lastSite AND $lastSite > 0)) { } #=============================================================================== -# Fetch user IDs from database +# Fetch items from database #=============================================================================== -$execSQL = "SELECT id FROM %s ORDER BY {$site_sort} LIMIT ".(($currentSite-1) * $site_size).", {$site_size}"; -$userIDs = $Database->query(sprintf($execSQL, User\Attribute::TABLE))->fetchAll($Database::FETCH_COLUMN); +$execSQL = "SELECT * FROM %s ORDER BY {$site_sort} LIMIT ".(($currentSite-1) * $site_size).", {$site_size}"; +$Statement = $Database->query(sprintf($execSQL, User\Attribute::TABLE)); #=============================================================================== # TRY: Template\Exception #=============================================================================== try { - foreach($userIDs as $userID) { + while($Attribute = $Statement->fetchObject('User\Attribute')) { try { - $User = User\Factory::build($userID); + $User = User\Factory::buildByAttribute($Attribute); $ItemTemplate = generateUserItemTemplate($User); $users[] = $ItemTemplate; |