2010-11-04 52 views
6

我对kohana的分页工作原理感到迷茫3.在任何地方Kohana 3都有分页的好例子吗?Kohana 3分页

回答

14
 // Get the total count of articles 
    $count = $this 
     ->_profil 
     ->articles 
     ->count_all(); 

    // Create the pagination object 
    $pagi = Pagination::factory(array(
     'items_per_page' => 4, 
     'total_items'  => $count, 
    )); 

    // Find actual articles 
    $articles = $this->_profil 
     ->articles 
     ->join_categories() 
     ->order_by('id','DESC') 
     ->limit($pagi->items_per_page) 
     ->offset($pagi->offset) 
     ->find_all(); 

,然后在视图中,你只是做

echo $pagi; // ofc, after passing the Pagination object to view 

这里会发生什么事是分页类用它查看的__toString()魔术方法来呈现,以显示分页所需的HTML。所有分页参数都可以在创建对象时修改(在我们的例子中,传递适当的键到传递给factory()方法的数组)。

分页的默认键是“page”(查询字符串),您也可以修改它。分页还有一个默认配置,您可以通过将其复制到应用程序/配置文件夹来覆盖它。

使用它:)

+1

也ORM具有有用'count_last_query(),用于计数结果行'方法。 – biakaveron 2010-11-05 19:40:02

+1

尽管在复杂查询时我不会太依赖这种方法:)通过这种方式,我们可以重新使用计数查询,并在计数发生前添加重置(FALSE)。 – Kemo 2010-11-05 21:28:16

+0

酷!这很有用,谢谢! – dana 2011-03-19 13:21:46

3

在Kohana 3.1分页不包括在内。下载模块并将其放入模块文件夹中。启用应用程序中的模块/ bootstrap.php。这是我的控制器页面。对于进一步的配置从模块复制所提供的配置文件/分页/配置/ pagination.php的application/config/pagination.php

$per_page =2; 
    $page_num = $this->request->param('page', 1); 
    $offset = ($page_num - 1) * $per_page; 
    $view =View::factory('image/imagelist')->bind('page_links',$page_links)->bind('results', $results)->bind('pagination', $pagination); 

    // Get the total count of records in the database 
    $userid = Auth::instance()->get_user()->pk(); 
    $count=ORM::factory('user_image')->where('app_userid','=',$userid)->count_all(); 


    // Create an instance of Pagination class and set values 
    $pagination = Pagination::factory(array( 

     'total_items' => $count, 
     'current_page' => array('source' => 'image/imagelist', 'key' => 'page'), 
     'items_per_page' => $per_page, 
     'offset' => $offset, 
     'view' => 'pagination/basic' 
)); 


     // Load specific results for current page 
    $results = DB::select()->from('user_images') 
      ->where('app_userid','=',$userid) 
      ->order_by('image_id','ASC') 
      ->limit($pagination->items_per_page) 
      ->offset($pagination->offset)->execute(); 

$page_links = $pagination; 
$this->template->content=$view->render(); 

你可能会得到错误ErrorException [注意]:未定义的属性:Request::$uri 。在分页类(模块)中。为了解决固定它

使用Request::current()->uri()代替Request::current()->uri