[Catalyst] iterating ResultSet in controller vs. template

John Goulah jgoulah at gmail.com
Tue Jun 19 21:08:23 GMT 2007


On 6/19/07, Matt S Trout <dbix-class at trout.me.uk> wrote:
>
> 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 =3D $rs->search(...);
>
> is exactly equivalent to
>
> my $result_rs =3D $rs->search(...);
> my @results =3D $rs->all;
>
> See
>
>
> http://search.cpan.org/~mstrout/DBIx-Class-0.08001/lib/DBIx/Class/ResultS=
et.pm#search
>
>
> http://search.cpan.org/~mstrout/DBIx-Class-0.08001/lib/DBIx/Class/ResultS=
et.pm#all
>
> Amazing what you can learn if you actually read the documentation :)




Thanks guys that all helps a lot, I did read the docs but somehow missed
that (or perhaps I saw it but figured there was some way to get from scalar
to array or vice versa here)-
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/catalyst/attachments/20070619/7238a=
a50/attachment.htm


More information about the Catalyst mailing list