Refactored pagination queries

This commit is contained in:
Marcin Kurczewski 2013-11-30 13:59:29 +01:00
parent 28dbb85b46
commit d7cb024f24
4 changed files with 30 additions and 23 deletions

View file

@ -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;

View file

@ -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;

View file

@ -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;

View file

@ -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()