phppaginationkohanakohana-orm

Use kohana relations (has_many etc..) with kohana-pagination


I'm using the kohana-pagination module by Morgan (guide) and I would like to know how can I use the relation between tables with this one.

Here is an example.

I have three tables categories, posts and categories_post (pivot table)

Categories has_many Posts through categories_post and Posts has_many Categories through categories_post

Fine, works perfect with kohana.

Now I would like to use the pagination module to display a list of posts and their categories. Usually I just JOIN the tables and then I fetch, but with Kohana + this module I don't know how to use it.

I've tried with this :

$paginate = Paginate::factory(ORM::factory('post')))->columns(array('id','categories.name'))->execute();

Solution

  • Be sure to setup the ORM object prior to initializing Paginate and you should have no issues using relationships. Keep in mind that Paginate is simply building atop your object prior to pulling a response.

    If the relationship is being handled in the ORM class, there is nothing special to do with Paginate. Recommend prefixing the column names with the table name ("post.id" for example). If you are joining outside of ORM, be sure to do so prior to passing ORM to Paginate:

    $post = ORM::factory('post')
        ->join('category')
        ->on('post.id', '=', 'category.post_id');
    
    $paginate = Paginate::factory($post)
        ->columns(array('post.id','category.name'))
        ->execute();
    

    I'm the creator and maintainer of Paginate and am happy to answer any questions you might have. Thank you.