[Catalyst] iterating twice over the same resultset in the template?

Brian Kirkbride brian.kirkbride at deeperbydesign.com
Wed Jan 3 19:58:26 GMT 2007


Fernan Aguero wrote:
> [ or perhaps 'is it possible to clone a resultset?' ]
> 
> Hi!
> 
> I'm building a form whose elements are items taken from the
> db.  And I need these items to show up in different elements
> (fieldsets) of the form. More or less something like this:
> 
> With items similar to:
>  'checkbox' - Item 1
>  'checkbox' - Item 2
>  'checkbox' - Item 3
> 
> with parts that belong to:
>  'checkbox' - Item 1
>  'checkbox' - Item 2
>  'checkbox' - Item 3
> 
> 
> In my controller:
> my $rs = $model->search( ... );
> $c->stash->{items} = $rs;
> 
> In my template (TT):
> <form ...>
> <fieldset>
> [% WHILE ( it = items.next() ) -%]
> <input ... > [% it.name -%]
> [% END -%]
> </fieldset>
> 
> <fieldset>
> [% WHILE ( it = items.next() ) -%]
> <input ... > [% it.name -%]
> [% END -%]
> </fieldset>
> </form>
> 
> The problem is that the resultset is empty when I try to
> iterate over it the second time. 
> 
> So far the only way I've found to make this work is
> to do N separate searches, fill N stash elements, et cetera 
> 
> $rs1 = $model->search( ... ); $c->stash->{rs1} = $rs1;
> $rs2 = $model->search( ... ); $c->stash->{rs2} = $rs2;
> ...
> 
> [% WHILE ( it = rs1.next() ) -%] ...
> [% WHILE ( it = rs2.next() ) -%] ...
> 
> Is there any other way to do this? Is it possible to clone a
> resultset?
> 
> Thanks,
> 
> Fernan


You want rs.reset() between the iterations IIRC.

Best,
Brian



More information about the Catalyst mailing list