Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Catalyst: Users

paging with Data::Page

 

 

Catalyst users RSS feed   Index | Next | Previous | View Threaded


anotheranne at fables

Jul 26, 2009, 8:14 AM

Post #1 of 12 (1891 views)
Permalink
paging with Data::Page

Hi,

In cannibalizing code from the (old) book on page 66 I first needed to
de-BindLex this but have not had total success. As authored it works
fine, but when I try to change the code to a standard stash operation I
end up with either it not outputting the text 'x of y' records, or the
paging putting all returned records onto one page instead of only 10 per
page.

I have no doubt my code is wrong somewhere.

I see that on p.67 it says that 'the pager is a Data::Page object'. I
don't see 'use Data::Page;' anywhere, nor can I see from perldoc
Data::Page anything relevant to the code in the book.

Are there any other sources where I might clarify this problem?

Maybe this is covered in the new book? I have it on order, but it takes
weeks to get here.

PS. is there any way to search the archives of this mailing list, short
of downloading the gzip files?

thanks in advance for any help
Anne

_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/


ian at sillit

Jul 26, 2009, 9:10 AM

Post #2 of 12 (1805 views)
Permalink
Re: paging with Data::Page [In reply to]

Hi Anne,

I don't have my copy of the book in front of me right now. However, either
way your best bet is to cut and paste your actual code so people on the list
can actually see what the problem is (e.g. the complete code of the method
in your controller and the relevant bit of your TT template would probably
be more than enough).

I see that on p.67 it says that 'the pager is a Data::Page object'. I
> don't see 'use Data::Page;' anywhere, nor can I see from perldoc
> Data::Page anything relevant to the code in the book.
>

My guess here is that you're using a DBIC model and the pager was being
accessed from DBIx::Class::ResultSet object - something like:

my $rs = $c->model( 'YourDBICModel::YourTable' )->search(
undef, # return all entries from the table
{
page => $current_page_number,
rows => $entries_per_page,
}
);

my $pager = $rs->pager; # isa Data::Page

There's a few different ways of doing this in DBIC though - so this may not
match up to your existing code.


http://search.cpan.org/~ribasushi/DBIx-Class-0.08108/lib/DBIx/Class/ResultSet.pm

PS. is there any way to search the archives of this mailing list, short
> of downloading the gzip files?
>

(footer of this email)

http://www.mail-archive.com/catalyst [at] lists/


Cheers,

Ian


anotheranne at fables

Jul 27, 2009, 4:43 AM

Post #3 of 12 (1792 views)
Permalink
Re: paging with Data::Page [In reply to]

hi, Ian

Thanks for advice, not always too keen to offload total problems for
free code input, but in this instance after lots of hours spent ...

# p.66 of Rockway Catalyst book.
# currently displaying 'x to y of z' record count, but not paging to 10
per page.

sub search : Global Form {
my ($self, $c, $query, $pager) = @_;
my $form = $self->formbuilder;
# get query from the URL (or the form if there is nothing there)
$query ||= $form->field('query')
if ($form->submitted && $form->validate);
return unless $query; #no query? we're done

$c->stash->{query} = $query;
my @tokens = split /\s+/, $query;
# my $result : Stashed; # original Bindlex line
my $result = $c->stash->{result}; # create stash entry
if('Names' eq $form->field('domain')) {
$result = $c->forward('search_names', \@tokens);
$c->stash(result => $result); # put data into stash
$c->stash->{'template'} = 'search/name_results.tt2'
}
else {
# note, finds in any field not just Addresses as implied
$result = $c->forward('search_addresses', \@tokens);
$c->stash(result => $result); # put data into stash
$c->stash->{'template'} = 'search/address_results.tt2'
}
my $page = $c->request->param('page');
$page = 1 if($page !~ /^\d+$/);
$result = $result->page($page);
# my $pager : Stashed = $result->pager;
# original Bindlex line
$c->stash->{pager} = $result->pager;
# Displays entries 'x to y of z'
# paging/10 if data a la Bindlex

# but with data Bindlex code replaced
# is Displaying entries 'x to y of z',
# not paging into 10 records per page

any ideas gratefully received
Anne

_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/


ian at sillit

Jul 27, 2009, 6:42 AM

Post #4 of 12 (1783 views)
Permalink
Re: paging with Data::Page [In reply to]

Thanks for advice, not always too keen to offload total problems for
> free code input, but in this instance after lots of hours spent ...
>

Quickest way to get the problem fixed.

I haven't got time to test the following just now, but my guess is that it
is how you are assigning the result:

- $c->stash(result => $result); # put data into stash

try changing that to:

+ $c->stash->{result} = $result; # put data into stash

Could be wrong, but I think the former is causing the resultset to be
evaluated as an array and is therefore returning all the entries before the
pager is being applied.

Cheers,

Ian


anotheranne at fables

Jul 27, 2009, 11:33 AM

Post #5 of 12 (1788 views)
Permalink
Re: paging with Data::Page [In reply to]

Ian, hi.

Thanks so much for the suggestion, see comment below.

On Mon, 27 Jul 2009 14:42:25 +0100
Ian Sillitoe <ian [at] sillit> wrote:

> Thanks for advice, not always too keen to offload total problems for
> > free code input, but in this instance after lots of hours spent ...
> >
>
> Quickest way to get the problem fixed.
>
> I haven't got time to test the following just now, but my guess is
> that it is how you are assigning the result:
>
> - $c->stash(result => $result); # put data into stash
>
> try changing that to:
>
> + $c->stash->{result} = $result; # put data into stash
>
> Could be wrong, but I think the former is causing the resultset to be
> evaluated as an array and is therefore returning all the entries
> before the pager is being applied.

Sad to say this has not cleared the issue (am sure have been this route
before) and though we still get '1 - 10 of 13 entries' all 13 are on the
one page still.

Noting the following, I agree that the 'pager' code is probably correct
and the 'result' code seems to be the gremlin. How they interact is
beyond me

1 The de-Bindlexed 'pager' code definitely pages the entries into sets
of 10 with the original Bindlex 'result' code.

2 But the de-Bindlexed 'result' code is not paged into sets of 10 with
the original Bindlex 'pager' code with either your line or mine

does anyone else have a suggestion?

I note that Bindlex used to bring up a warning when the catalyst
development server was started, but I do not see this now. Has Bindlex
perhaps been un-deprecated? - in which case I can save all this effort
and continue using it!
>
> Cheers,
>
> Ian

_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/


michael.reddick at gmail

Jul 27, 2009, 12:10 PM

Post #6 of 12 (1789 views)
Permalink
Re: paging with Data::Page [In reply to]

>
>
> does anyone else have a suggestion?
>
>
try this:
$c->stash->{result} = $result = $result->page($page)


ian at sillit

Jul 27, 2009, 1:32 PM

Post #7 of 12 (1778 views)
Permalink
Re: paging with Data::Page [In reply to]

>
> Could be wrong, but I think the former is causing the resultset to be
> evaluated as an array and is therefore returning all the entries
> before the pager is being applied.
>

Apologies - I was talking rubbish about the usage of stash being the cause
(although I do think the resultset is being reset somewhere along the line).

Noting the following, I agree that the 'pager' code is probably correct
> and the 'result' code seems to be the gremlin. How they interact is
> beyond me


$result is a DBIx::Class::ResultSet object and is being returned from the
call to $c->model('Address::Addresses')->search(). This resultset object has
a method called $result->page($page_number) which sets up the database query
with the correct start and stop for the given page number and entries per
page (default=10). It also has a $result->pager() method which returns a
Data::Page object - this just provides a useful container for accessing
page-related information of the related DB query.


http://search.cpan.org/~ribasushi/DBIx-Class-0.08108/lib/DBIx/Class/ResultSet.pm

If the $pager is telling you the correct information (that the query only
has 10 entries) but this is changing by the time it is being rendered in
your template then perhaps another call is being made to the database (i.e.
because something else is changing $c->stash->result). Have you tried
setting DBIC_TRACE=1 in your environment and looking at the logs to check
that the database queries look sensible?

You could also check that the resultset at least has the correct number of
entries just before you leave your search function:

$c->log->info( "Current Page: " . $page );
$c->log->info( "Results: " . $result->count ); # NB: this will perform a
DB query
$c->log->info( "Leaving 'search' method..." );

I note that Bindlex used to bring up a warning when the catalyst
> development server was started, but I do not see this now. Has Bindlex
> perhaps been un-deprecated?
>

Nope -
http://search.cpan.org/~mstrout/Catalyst-Controller-BindLex-0.05/lib/Catalyst/Controller/BindLex.pm


Cheers,

Ian


michael.reddick at gmail

Jul 27, 2009, 4:18 PM

Post #8 of 12 (1780 views)
Permalink
Re: paging with Data::Page [In reply to]

>
> If the $pager is telling you the correct information (that the query only
> has 10 entries) but this is changing by the time it is being rendered in
> your template then perhaps another call is being made to the database (i.e.
> because something else is changing $c->stash->result).
>

Based on the docs, it looks like calling ->page doesn't affect the resultset
you call it on but it returns another one with the paging. So you need to
set the result stash variable to the return value of the ->page call.


anotheranne at fables

Jul 28, 2009, 11:57 AM

Post #9 of 12 (1780 views)
Permalink
Re: paging with Data::Page [In reply to]

Hi, Ian

See below,

On Mon, 27 Jul 2009 21:32:36 +0100
Ian Sillitoe <ian [at] sillit> wrote:

> >
> > Could be wrong, but I think the former is causing the resultset to
> > be evaluated as an array and is therefore returning all the entries
> > before the pager is being applied.
> >
>
> Apologies - I was talking rubbish about the usage of stash being the
> cause (although I do think the resultset is being reset somewhere
> along the line).
>
> Noting the following, I agree that the 'pager' code is probably
> correct
> > and the 'result' code seems to be the gremlin. How they interact is
> > beyond me
>
>
> $result is a DBIx::Class::ResultSet object and is being returned from
> the call to $c->model('Address::Addresses')->search(). This resultset
> object has a method called $result->page($page_number) which sets up
> the database query with the correct start and stop for the given page
> number and entries per page (default=10). It also has a
> $result->pager() method which returns a Data::Page object - this just
> provides a useful container for accessing page-related information of
> the related DB query.
>
>
> http://search.cpan.org/~ribasushi/DBIx-Class-0.08108/lib/DBIx/Class/ResultSet.pm
>
> If the $pager is telling you the correct information (that the query
> only has 10 entries) but this is changing by the time it is being
> rendered in your template then perhaps another call is being made to
> the database (i.e. because something else is changing
> $c->stash->result). Have you tried setting DBIC_TRACE=1 in your
> environment and looking at the logs to check that the database
> queries look sensible?
>
> You could also check that the resultset at least has the correct
> number of entries just before you leave your search function:
>
> $c->log->info( "Current Page: " . $page );
> $c->log->info( "Results: " . $result->count ); # NB: this will
> perform a DB query
> $c->log->info( "Leaving 'search' method..." );

this produces 2 items of note if I put it after the search

1. 'Use of uninitialized value $page in concatenation (.) or
string at ....'
($page is declared in the line .... = @_)

2. [info] Current Page:
[info] Results: 11
[info] Leaving 'search' method...
--------------------------------------------

but if I put it after this line
$page = 1 ; #if($page !~ /^\d+$/);

I get

1. 'Use of uninitialized value $page in pattern match (m//)
at ... '
(a SELECT COUNT( * ) FROM occurs immediately after this )

lower down, we get the run of outputs from
SELECT me.id,

which match up fine to the records expected and delivered, ( I set
DBIC_TRACE=1). Should these not show up before the pattern match
line?

2. then just at the end

[info] Current Page: 1
[info] Results: 11
[info] Leaving 'search' method...

so pages are assigned in the pager code, but the no of results is
carried over from the search code

These things have a way of getting more complex, so if this info does
not indicate a clear solution, then I don't want to waste valuable time,
rather I might find more in the new book, and must advance further in my
perl studies (not very far into Intermediate Perl at present). Although
I hate being beaten, TIMTOWTDI (whatever)

Anne

>
> I note that Bindlex used to bring up a warning when the catalyst
> > development server was started, but I do not see this now. Has
> > Bindlex perhaps been un-deprecated?
> >
>
> Nope -
> http://search.cpan.org/~mstrout/Catalyst-Controller-BindLex-0.05/lib/Catalyst/Controller/BindLex.pm
>
>
> Cheers,
>
> Ian

_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/


anotheranne at fables

Jul 28, 2009, 11:58 AM

Post #10 of 12 (1767 views)
Permalink
Re: paging with Data::Page [In reply to]

Thanks, Michael,

On Mon, 27 Jul 2009 14:10:23 -0500
Michael Reddick <michael.reddick [at] gmail> wrote:

> >
> >
> > does anyone else have a suggestion?
> >
> >
> try this:
> $c->stash->{result} = $result = $result->page($page)

Nope:(

no change, more detail on happenings in reply to Ian

Anne

_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/


ian at sillit

Jul 29, 2009, 3:55 AM

Post #11 of 12 (1760 views)
Permalink
Re: paging with Data::Page [In reply to]

Hi Anne,

> try this:
> > $c->stash->{result} = $result = $result->page($page)
>

I figured Michael's answer would sort it actually - that looked exactly the
sort of thing that would could cause the problem you noticed (my bad for not
reading the DBIC::RS->page() docs thorough).

Probably got to a point where the best thing to do is tar/zip up your entire
app and send it over (ian.sillitoe at gmail) - probably much quicker than
pinging debug suggestions back and forwards - I'm happy to have a look
whenever I get a chance.

Cheers,

Ian


anotheranne at fables

Jul 29, 2009, 9:46 AM

Post #12 of 12 (1751 views)
Permalink
Re: paging with Data::Page [In reply to]

Thanks Ian,

That's cool, but let's hold on a while to see if I can hack it from
further examination and knowledge to be gained when I get the new
Catalyst book.

I would like to sort it if only for the reason that i can then offer
correct code for all the book examples up to that point. It might
be good to archive them somewhere. The book was (still is) advertised
with glowing testimonials and that would be a good way of extending its
useful life as a starter text.

Watch this space.

Regards
Anne

On Wed, 29 Jul 2009 11:55:33 +0100
Ian Sillitoe <ian [at] sillit> wrote:

> Hi Anne,
>
> > try this:
> > > $c->stash->{result} = $result = $result->page($page)
> >
>
> I figured Michael's answer would sort it actually - that looked
> exactly the sort of thing that would could cause the problem you
> noticed (my bad for not reading the DBIC::RS->page() docs thorough).
>
> Probably got to a point where the best thing to do is tar/zip up your
> entire app and send it over (ian.sillitoe at gmail) - probably much
> quicker than pinging debug suggestions back and forwards - I'm happy
> to have a look whenever I get a chance.
>
> Cheers,
>
> Ian

_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/

Catalyst users RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.