diff --git a/src/Controllers/CommentController.php b/src/Controllers/CommentController.php index 3cfaf3be..1b154c73 100644 --- a/src/Controllers/CommentController.php +++ b/src/Controllers/CommentController.php @@ -20,10 +20,9 @@ class CommentController $this->context->subTitle = 'comments'; PrivilegesHelper::confirmWithException(Privilege::ListComments); - $commentCount = Model_Comment::getEntityCount(null); + $page = max(1, $page); + list ($comments, $commentCount) = Model_Comment::getEntitiesWithCount(null, $commentsPerPage, $page); $pageCount = ceil($commentCount / $commentsPerPage); - $page = max(1, min($pageCount, $page)); - $comments = Model_Comment::getEntities(null, $commentsPerPage, $page); R::preload($comments, ['commenter' => 'user', 'post', 'post.uploader' => 'user', 'post.sharedTag']); $this->context->postGroups = true; diff --git a/src/Controllers/PostController.php b/src/Controllers/PostController.php index d3410226..3c5e72cd 100644 --- a/src/Controllers/PostController.php +++ b/src/Controllers/PostController.php @@ -103,10 +103,10 @@ class PostController $this->context->massTagQuery = $query; } - $postCount = Model_Post::getEntityCount($query); + $page = max(1, $page); + list($posts, $postCount) = Model_Post::getEntitiesWithCount($query, $postsPerPage, $page); $pageCount = ceil($postCount / $postsPerPage); - $page = max(1, min($pageCount, $page)); - $posts = Model_Post::getEntitiesFast($query, $postsPerPage, $page); + $page = min($pageCount, $page); R::preload($posts, 'sharedTag'); $this->context->transport->paginator = new StdClass; diff --git a/src/Controllers/UserController.php b/src/Controllers/UserController.php index e9461d08..4f77295b 100644 --- a/src/Controllers/UserController.php +++ b/src/Controllers/UserController.php @@ -128,10 +128,9 @@ class UserController $this->context->subTitle = 'users'; PrivilegesHelper::confirmWithException(Privilege::ListUsers); - $userCount = Model_User::getEntityCount($sortStyle); + $page = max(1, $page); + list ($users, $userCount) = Model_User::getEntitiesWithCount($sortStyle, $usersPerPage, $page); $pageCount = ceil($userCount / $usersPerPage); - $page = max(1, min($pageCount, $page)); - $users = Model_User::getEntities($sortStyle, $usersPerPage, $page); $this->context->sortStyle = $sortStyle; $this->context->transport->paginator = new StdClass; @@ -437,10 +436,9 @@ class UserController else throw new SimpleException('Wrong tab'); - $postCount = Model_Post::getEntityCount($query); + $page = max(1, $page); + list ($posts, $postCount) = Model_Post::getEntitiesWithCount($query, $postsPerPage, $page); $pageCount = ceil($postCount / $postsPerPage); - $page = max(1, min($pageCount, $page)); - $posts = Model_Post::getEntities($query, $postsPerPage, $page); R::preload($posts, 'sharedTag'); $this->context->transport->tab = $tab; diff --git a/src/Models/AbstractModel.php b/src/Models/AbstractModel.php index c87907ac..bb6cb416 100644 --- a/src/Models/AbstractModel.php +++ b/src/Models/AbstractModel.php @@ -31,18 +31,11 @@ abstract class AbstractModel extends RedBean_SimpleModel return $rows; } - public static function getEntities($query, $perPage = null, $page = 1) + protected static function convertRows($rows, $table, $fast = false) { - $table = static::getTableName(); - $rows = self::getEntitiesRows($query, $perPage, $page); - $entities = R::convertToBeans($table, $rows); - return $entities; - } + if (!$fast) + return R::convertToBeans($table, $rows); - public static function getEntitiesFast($query, $perPage = null, $page = 1) - { - $table = static::getTableName(); - $rows = self::getEntitiesRows($query, $perPage, $page); $entities = R::dispense($table, count($rows)); reset($entities); foreach ($rows as $row) @@ -51,6 +44,15 @@ abstract class AbstractModel extends RedBean_SimpleModel $entity->import($row); next($entities); } + reset($entities); + return $entities; + } + + public static function getEntities($query, $perPage = null, $page = 1, $fast = false) + { + $table = static::getTableName(); + $rows = self::getEntitiesRows($query, $perPage, $page); + $entities = self::convertRows($rows, $table, $fast); return $entities; } @@ -64,7 +66,15 @@ abstract class AbstractModel extends RedBean_SimpleModel $builder::build($dbQuery, $query); else $dbQuery->from($table); - return intval($dbQuery->get('row')['count']); + $ret = intval($dbQuery->get('row')['count']); + return $ret; + } + + public static function getEntitiesWithCount($query, $perPage = null, $page = 1) + { + $entities = self::getEntities($query, $perPage, $page, true); + $count = self::getEntityCount($query); + return [$entities, $count]; } public static function create()