From 0001d38699616c26f2ca4f5f9c6a4ac0c9fd33d1 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Sat, 30 Nov 2013 19:07:39 +0100 Subject: [PATCH] Further tweaks to model - Fixed broken negative searches - Faster search by tag / comment / submit / favorites (useless nested joins replaced to entity prefetch). Side effect: searching for nonexistent tags, users etc yields informative errors instead of "no posts to show") - Fixed duplicated column in order clause ("ORDER BY id DESC, id DESC") --- src/Models/AbstractModel.php | 10 +++-- src/Models/Model_Post_QueryBuilder.php | 52 +++++++++++--------------- 2 files changed, 29 insertions(+), 33 deletions(-) diff --git a/src/Models/AbstractModel.php b/src/Models/AbstractModel.php index ba4011c4..fa5507f9 100644 --- a/src/Models/AbstractModel.php +++ b/src/Models/AbstractModel.php @@ -33,16 +33,20 @@ abstract class AbstractModel extends RedBean_SimpleModel protected static function convertRows($rows, $table, $fast = false) { + if (empty($rows)) + return []; + if (!$fast) return R::convertToBeans($table, $rows); $entities = R::dispense($table, count($rows)); - reset($entities); + if (count($rows) == 1) + $entities = [$entities]; + $entity = reset($entities); foreach ($rows as $row) { - $entity = current($entities); $entity->import($row); - next($entities); + $entity = next($entities); } reset($entities); return $entities; diff --git a/src/Models/Model_Post_QueryBuilder.php b/src/Models/Model_Post_QueryBuilder.php index 570f3957..a0c80f25 100644 --- a/src/Models/Model_Post_QueryBuilder.php +++ b/src/Models/Model_Post_QueryBuilder.php @@ -40,15 +40,14 @@ class Model_Post_QueryBuilder implements AbstractQueryBuilder protected static function filterTag($dbQuery, $val) { + $tag = Model_Tag::locate($val); $dbQuery ->exists() ->open() ->select('1') ->from('post_tag') - ->innerJoin('tag') - ->on('post_tag.tag_id = tag.id') ->where('post_id = post.id') - ->and('LOWER(tag.name) = LOWER(?)')->put($val) + ->and('post_tag.tag_id = ?')->put($tag->id) ->close(); } @@ -187,9 +186,8 @@ class Model_Post_QueryBuilder implements AbstractQueryBuilder { list ($timeMin, $timeMax) = self::__filterTokenDateParser($val); $dbQuery - ->addSql('upload_date >= ?')->and('upload_date <= ?') - ->put($timeMin) - ->put($timeMax); + ->addSql('upload_date >= ?')->put($timeMin) + ->and('upload_date <= ?')->put($timeMax); } protected static function filterTokenDateMin($context, $dbQuery, $val) @@ -206,15 +204,14 @@ class Model_Post_QueryBuilder implements AbstractQueryBuilder protected static function filterTokenFav($context, $dbQuery, $val) { + $user = Model_User::locate($val); $dbQuery ->exists() ->open() ->select('1') ->from('favoritee') - ->innerJoin('user') - ->on('favoritee.user_id = user.id') ->where('post_id = post.id') - ->and('LOWER(user.name) = LOWER(?)')->put($val) + ->and('favoritee.user_id = ?')->put($user->id) ->close(); } @@ -225,15 +222,14 @@ class Model_Post_QueryBuilder implements AbstractQueryBuilder protected static function filterTokenComment($context, $dbQuery, $val) { + $user = Model_User::locate($val); $dbQuery ->exists() ->open() ->select('1') ->from('comment') - ->innerJoin('user') - ->on('commenter_id = user.id') ->where('post_id = post.id') - ->and('LOWER(user.name) = LOWER(?)')->put($val) + ->and('commenter_id = ?')->put($user->id) ->close(); } @@ -244,13 +240,8 @@ class Model_Post_QueryBuilder implements AbstractQueryBuilder protected static function filterTokenSubmit($context, $dbQuery, $val) { - $dbQuery - ->addSql('uploader_id = ') - ->open() - ->select('user.id') - ->from('user') - ->where('LOWER(name) = LOWER(?)')->put($val) - ->close(); + $user = Model_User::locate($val); + $dbQuery->addSql('uploader_id = ?')->put($user->id); } protected static function filterTokenUploader($context, $dbQuery, $val) @@ -384,17 +375,15 @@ class Model_Post_QueryBuilder implements AbstractQueryBuilder { $unparsedTokens = []; - foreach ($tokens as $token) + foreach ($tokens as $origToken) { + $token = $origToken; + $neg = false; if ($token{0} == '-') { $token = substr($token, 1); $neg = true; } - else - { - $neg = false; - } $pos = strpos($token, ':'); if ($pos === false) @@ -411,7 +400,7 @@ class Model_Post_QueryBuilder implements AbstractQueryBuilder $parsed = $callback($neg, $key, $val); if (!$parsed) - $unparsedTokens []= $token; + $unparsedTokens []= $origToken; } return $unparsedTokens; } @@ -485,10 +474,13 @@ class Model_Post_QueryBuilder implements AbstractQueryBuilder else $dbQuery->asc(); - $dbQuery->addSql(', id '); - if ($context->orderDir == 1) - $dbQuery->desc(); - else - $dbQuery->asc(); + if ($context->orderColumn != 'id') + { + $dbQuery->addSql(', id'); + if ($context->orderDir == 1) + $dbQuery->desc(); + else + $dbQuery->asc(); + } } }