From 1e954bb815dddd5ce89fbaa8be5aee16189a42dd Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Sat, 30 Nov 2013 00:10:31 +0100 Subject: [PATCH] Optimalization: faster but dirty row retrieval --- src/Controllers/PostController.php | 2 +- src/Models/AbstractModel.php | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/Controllers/PostController.php b/src/Controllers/PostController.php index 14fded91..4c27a0b7 100644 --- a/src/Controllers/PostController.php +++ b/src/Controllers/PostController.php @@ -106,7 +106,7 @@ class PostController $postCount = Model_Post::getEntityCount($query); $pageCount = ceil($postCount / $postsPerPage); $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->page = $page; diff --git a/src/Models/AbstractModel.php b/src/Models/AbstractModel.php index f066b2c8..336501ac 100644 --- a/src/Models/AbstractModel.php +++ b/src/Models/AbstractModel.php @@ -26,6 +26,7 @@ abstract class AbstractModel extends RedBean_SimpleModel $dbQuery->limit('?')->put($perPage); $dbQuery->offset('?')->put(($page - 1) * $perPage); } + $rows = $dbQuery->get(); return $rows; } @@ -38,6 +39,21 @@ abstract class AbstractModel extends RedBean_SimpleModel 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) { $table = static::getTableName();