[Catalyst] DBIx::Class/TT Pager Question

Hermida, Leandro Leandro.Hermida at fmi.ch
Sat Dec 30 18:21:50 GMT 2006


Hello,
 
Maybe you should try this...  You should know what page your user wants
because they've clicked on a link to display a certain page (or just
default to the first page if parameter doesn't exist which DBIx::Class
will do).  So your action should have access to what page to display via
$c->req->params('page').  Then in the controller you get all of the
photo objects for that particular page (notice the ->all method at the
end of the search) and put that in your stash for TT.
 
my $page = $c->req->params('page') || '';
 
$c->stash->{photos} = [$c->model('CatapultDB::Photos')->search(
    {
      'gallery.id' => $selected_gallery
    },
    {
      join     => [qw/ gallery /],
      prefetch => [qw/ gallery /],
      rows     => 2,
      page     => $page,
    }
)->all];
 
Then in TT code just like you wrote:
 
[% FOREACH photo IN photos -%]
[% photo.gallery.name %]
[% END -%]
 
 
I hope this might help?
 
Leandro
 



	
________________________________

	From: Dennis Daupert [mailto:ddaupert at sbcglobal.net] 
	Sent: Friday, December 29, 2006 22:38
	To: catalyst at lists.rawmode.org
	Subject: Re: [Catalyst] DBIx::Class/TT Pager Question
	
	
	
	Leandro:
	> you don't need to use Data::Page directly 
	
	yes, you are quite right, I took out the
	explicit call to Data::Page; I set rows => 2,
	and my query gets 2 thumbs. Cool.
	
	>you are setting $c->stash->{photos} to an 
	>array reference when you do 
	>$c->stash->{photos} = [$c->model( ... ]; 
	>and then you are later trying to make a 
	>method call ->pager() on that array reference, 
	>not an object. 
	
	With the square brackets, I get an array ref, 
	and in my template, I do stuff like:
	[% FOREACH photo IN photos -%]
	[% photo.gallery.name %]
	[% END -%]
	
	When I take the square brackets out of the
	query, my template doesn't display anything
	anymore. 
	
	>See the "Paged Results" section of the 
	>DBIx::Class::Manual::Cookbook POD on
	>CPAN.
	
	which of course I'd been trying to beat 
	into my forehead. Cookbook says:
	"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();"
	
	So I'm trying to call the pager method from the
	resultset. But translating from the cookbook
	example (which is non-Catalyst centric) to 
	Cat code, falls the shadow.
	
	Chisel:
	>$c->stash->{pager} => $c->stash->{photos}->pager();
	>shouldn't this be an assignment rather than a fat-comma?
	
	Good eyeballs! Yes. 
	
	>I think you need to lose the square brackets, 
	>and you will probably find you then have a 
	>DBIx::Class::ResultSet, which does have the 
	>pager() method. 
	
	When I do, I get this error:
	Caught exception in
Catapult::Controller::Photo::Gallery->display
"DBIx::Class::ResultSet::pager(): Can't create pager for non-paged rs
	
	Going back to the DBIx::Class Cookbook example, 
	which is pretty simple (no joins or anything), do I 
	have the joins and the row settings in the right place? 
	I tried a few combos, and the one that at least doesn't 
	throw cat down the stairs is the one I have been using:
	
	    $c->stash->{photos} =
[$c->model('CatapultDB::Photos')->search(
	      {
	        'gallery.id' => $selected_gallery
	      },
	      {
	        join     => [qw/ gallery /],
	        prefetch => [qw/ gallery /],
	        rows => 2,
	      }
	    )];
	
	Much less hair now,
	
	/dennis
	
	
	

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/catalyst/attachments/20061230/225bf135/attachment.htm


More information about the Catalyst mailing list