Howdy all,

I'm having some trouble with DBIx::Class. I'm trying to paginate results, 10 results per page, nothing special.

However, what i'm running into is

  1. the results aren't being ordered correctly, it's ordering them ASCendingly when i've specified DESCending results in the query and
  2. it's grouping the results (or something) so that there ARE 10 results per page, instead of giving me 10 results on one page, then the remaining 6 on the next (i'm testing with 16 records)
Instead, it gives me 10 on each page, and I can't really tell how it orders them after you click on the "next" link.

I'm honestly baffled, I don't know how this setup is supposed to work, or how to change the paged results, the docs only say:

When you expect a large number of results, you can ask DBIx::Class for a paged resultset, which will fetch only a small number of records at a time:

my $rs = $schema->resultset('Artist')->search(
undef,
{
page => 1, # page to return (defaults to 1)
rows => 10, # number of results per page
},
);

return $rs->all(); # all records for page 1

The page attribute does not have to be specified in your search:

my $rs = $schema->resultset('Artist')->search(
undef,
{
rows => 10,
}
);

return $rs->page(1); # DBIx::Class::ResultSet containing first 10 records

In either of the above cases, you can return a Data::Page object for the resultset (suitable for use in e.g. a template) using the pager method:

return $rs->pager();

return $rs->page(1); # DBIx::Class::ResultSet containing first 10 records <- this doesn't work either. Currently, I have something like this:

sub main { my $self = shift; my $tmpl = $self->load_tmpl( 'index.html', %config_vars ); my $q = $self->query; my $comments = $schema->resultset('News')->search( undef, { rows => 10, page => $q->param('next_page'), order_by => 'date DESC' } ); ## let's populate @comments for ( $comments->all ) { my %row_data; #$row_data{'comment_id'} = $_->comment_id; $row_data{'post_subject'} = $_->subject; $row_data{'post_author'} = $_->author; $row_data{'post_date'} = $_->date; $row_data{'post_id'} = $_->postid; $row_data{'post_content'} = $_->story; #$row_data{'is_hidden'} = $_->is_hidden; # push it into the the loop.... push ( @end_posts, \%row_data ); } $tmpl->param( site_title => $self->param('main_page_title'), site_css => 'css/main.css', post_data => \@end_posts, next_page => $comments->pager->next_page, previous_page => $comments->pager->previous_page, ); $tmpl->output; }

All I can think of is maybe  page => $q->param('next_page'), this attribute is somehow effing things up, but it's creating the necessary two pages...so I really can't say.

I've scoured all the docs and I just can't find anything. Am I missing something monks?

UPDATE:Looks like changing page => $q->param('next_page') to page => $q->param('next_page') || 1 and

my $comments = $schema->resultset('News')->search( undef, { rows => 10, page => $q->param('next_page'), order_by => 'date DESC' } );
to
my $comments = $schema->resultset('News')->search( {}, { rows => 10, page => $q->param('next_page'), order_by => 'date DESC' } );
did the trick...(thanks LTJake :-))

meh.

In reply to DBIx::Class Pagination troubles by stonecolddevin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.