Optimalization: faster but dirty row retrieval

This commit is contained in:
Marcin Kurczewski 2013-11-30 00:10:31 +01:00
parent 518311ff61
commit 1e954bb815
2 changed files with 17 additions and 1 deletions

View file

@ -106,7 +106,7 @@ class PostController
$postCount = Model_Post::getEntityCount($query); $postCount = Model_Post::getEntityCount($query);
$pageCount = ceil($postCount / $postsPerPage); $pageCount = ceil($postCount / $postsPerPage);
$page = max(1, min($pageCount, $page)); $page = max(1, min($pageCount, $page));
$posts = Model_Post::getEntities($query, $postsPerPage, $page); $posts = Model_Post::getEntitiesFast($query, $postsPerPage, $page);
$this->context->transport->paginator = new StdClass; $this->context->transport->paginator = new StdClass;
$this->context->transport->paginator->page = $page; $this->context->transport->paginator->page = $page;

View file

@ -26,6 +26,7 @@ abstract class AbstractModel extends RedBean_SimpleModel
$dbQuery->limit('?')->put($perPage); $dbQuery->limit('?')->put($perPage);
$dbQuery->offset('?')->put(($page - 1) * $perPage); $dbQuery->offset('?')->put(($page - 1) * $perPage);
} }
$rows = $dbQuery->get(); $rows = $dbQuery->get();
return $rows; return $rows;
} }
@ -38,6 +39,21 @@ abstract class AbstractModel extends RedBean_SimpleModel
return $entities; return $entities;
} }
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)
{
$entity = current($entities);
$entity->import($row);
next($entities);
}
return $entities;
}
public static function getEntityCount($query) public static function getEntityCount($query)
{ {
$table = static::getTableName(); $table = static::getTableName();