Kohana 3.1 查询计数和查询分页

发布于 2024-10-28 00:41:43 字数 601 浏览 2 评论 0原文

我正在接触 Kohana,但在分页方面遇到了麻烦。我收到以下错误:

ErrorException [致命错误]:类 未找到“分页”

在非官方维基之后

Kohana::modules(array( 'database' => MODPATH.'database', 'userguide' => MODPATH.'userguide', 'pagination' => MODPATH.'pagination', ))

我修改了引导文件以包含此内容:但这似乎没有帮助。

我的第二个问题是关于查询计数......我很惊讶没有像 $query-count() 这样的函数,除非我选择 ORM 相反,我发现这个解决方案有点笨拙,因为查询计数是每个分页请求:

$result['count'] = $pagination_query->select('COUNT("*") AS result_count')->execute()->get('result_count');

有什么建议吗?

非常感谢

I am getting my feet wet with Kohana but having trouble with pagination. i get the following error :

ErrorException [ Fatal Error ]: Class
'Pagination' not found

following the unoffical wiki I amended the bootstrap file to include this:

Kohana::modules(array( 'database' => MODPATH.'database', 'userguide' => MODPATH.'userguide', 'pagination' => MODPATH.'pagination', ))

but that didn't seem to help.

my second question is with regards to query count.... I am surprised there is no function like $query-count() unless i opt for ORM instead i find this solution a bit clunky given that a query count is a must for every pagination request:

$result['count'] = $pagination_query->select('COUNT("*") AS result_count')->execute()->get('result_count');

Any suggestions?

thank you very much

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月野兔 2024-11-04 00:41:43

Kohana 3.1 没有附带分页模块...
下载

必须从https://github.com/kohana/pagination

,然后转到 class/kohana编辑第 199 行,从 ->uri 到 ->uri(),

进行操作

它对查询计数

......仍在搜索。希望这对某人有帮助

Kohana 3.1 does not come with the pagination module...
it must be downloaded from

https://github.com/kohana/pagination

then go to the class/kohana edit line 199 from ->uri to ->uri()

that does it

as to the query count....still searching.

hope this helps someone

温馨耳语 2024-11-04 00:41:43

Database 类中曾经有一个 count_last_query() 函数,它提供最后一次查询运行的总结果,因为它没有任何限制或偏移,但他们从版本 3.0.9 中提取了它。您可以在这里找到它的文档:

http://kohanaframework.org/3.0/guide/ api/Database#count_last_query

我实际上已经在该函数的代码的基础上构建了我自己的计数查询函数(如果您想使用它)。

protected static function _pagedQuery($query) {
  $sql = (string)$query;
  if (stripos($sql, 'LIMIT') !== FALSE) {
    // Remove LIMIT from the SQL
    $sql = preg_replace('/\sLIMIT\s+[^a-z]+/i', ' ', $sql);
  }
  if (stripos($sql, 'OFFSET') !== FALSE) {
    // Remove OFFSET from the SQL
    $sql = preg_replace('/\sOFFSET\s+\d+/i', '', $sql);
  }
  if (stripos($sql, 'ORDER BY') !== FALSE) {
    // Remove ORDER BY from the SQL
    $sql = preg_replace('/\sORDER BY\s+`\w+`(\.`\w+`)?(\s+DESC|\s+ASC)?/i', '', $sql);
  }

  $db = Database::instance();
  $result = $db->query(Database::SELECT, '
    SELECT COUNT(*) AS ' . $db->quote_identifier('total_rows') . '
    FROM (' . $sql . ') AS ' . $db->quote_table('counted_results'),
    TRUE
  );
  return (int)$result->current()->total_rows;
}

There used to be a count_last_query() function in the Database class which provided the total results of the last query run as it would be without any limit or offset, but they pulled it from version 3.0.9. You can find the documentation of it here:

http://kohanaframework.org/3.0/guide/api/Database#count_last_query

I've actually built upon the code from that function to make my own count query function if you want to use that.

protected static function _pagedQuery($query) {
  $sql = (string)$query;
  if (stripos($sql, 'LIMIT') !== FALSE) {
    // Remove LIMIT from the SQL
    $sql = preg_replace('/\sLIMIT\s+[^a-z]+/i', ' ', $sql);
  }
  if (stripos($sql, 'OFFSET') !== FALSE) {
    // Remove OFFSET from the SQL
    $sql = preg_replace('/\sOFFSET\s+\d+/i', '', $sql);
  }
  if (stripos($sql, 'ORDER BY') !== FALSE) {
    // Remove ORDER BY from the SQL
    $sql = preg_replace('/\sORDER BY\s+`\w+`(\.`\w+`)?(\s+DESC|\s+ASC)?/i', '', $sql);
  }

  $db = Database::instance();
  $result = $db->query(Database::SELECT, '
    SELECT COUNT(*) AS ' . $db->quote_identifier('total_rows') . '
    FROM (' . $sql . ') AS ' . $db->quote_table('counted_results'),
    TRUE
  );
  return (int)$result->current()->total_rows;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文