[Catalyst] iterating ResultSet in controller vs. template

Matt S Trout dbix-class at trout.me.uk
Tue Jun 19 20:51:09 GMT 2007


On Tue, Jun 19, 2007 at 08:28:48PM +0100, Daniel Hulme wrote:
> On Tue, Jun 19, 2007 at 03:09:29PM -0400, John Goulah wrote:
> > You are correct (and I did explain that as well in my original post that was
> > happening).  So the question is , how can I make the same call and access
> > the data in both places?  It seems silly that I have to make the call
> > without brackets to access in the controller, and with brackets to access in
> > the template, so I assume I am accessing the object incorrectly in one place
> > or the other, but I would like to be able to use ->next in the controller.
> 
> You don't get both the iteratorish and the arrayish behaviour. If you
> call it in list context, you get a list, and you can iterate over it
> using FOREACH in your template and `for my $alumnus (@$alumni)` in your
> Perl code. If you call it in scalar context, you get an iterator, and
> you can iterate over it by calling $alumnus->next in your controller and
> alumnus.next in your template. If you really want it both ways, the
> obvious way is to call the thing twice, once in scalar context and once
> in list context, and only put one of them in the stash. Or you could
> call it in scalar context and when you iterate over the results in your
> controller add them to a list as well and put the arrayref in the stash.
> There may be a proper way to convert an iteratorish result into a list:
> I suspect the people on the DBIx::Class mailing list would know.

my @results = $rs->search(...);

is exactly equivalent to

my $result_rs = $rs->search(...);
my @results = $rs->all;

See

http://search.cpan.org/~mstrout/DBIx-Class-0.08001/lib/DBIx/Class/ResultSet.pm#search

http://search.cpan.org/~mstrout/DBIx-Class-0.08001/lib/DBIx/Class/ResultSet.pm#all

Amazing what you can learn if you actually read the documentation :)

-- 
      Matt S Trout       Need help with your Catalyst or DBIx::Class project?
   Technical Director    Want a managed development or deployment platform?
 Shadowcat Systems Ltd.  Contact mst (at) shadowcatsystems.co.uk for a quote
http://chainsawblues.vox.com/             http://www.shadowcatsystems.co.uk/ 



More information about the Catalyst mailing list