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

2007-01-03 Thread Fernan Aguero
[ 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


___
List: Catalyst@lists.rawmode.org
Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@lists.rawmode.org/
Dev site: http://dev.catalyst.perl.org/


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

2007-01-03 Thread Jonathan Rockway
 [% WHILE ( it = rs1.next() ) -%] ...
 [% WHILE ( it = rs2.next() ) -%] ...

 Is there any other way to do this? Is it possible to clone a
 resultset?


Why not just prefetch everything?

Controller:

my @elements : Stashed = $c-model(MyDB::table)-...-all;

Template:

[% FOREACH element = elements %]
...
[% END %]



-- 
package JAPH;use Catalyst qw/-Debug/;($;=JAPH)-config(name = do {
$,.=reverse qw[Jonathan tsu rehton lre rekca Rockway][$_].[split //,
;$;]-[$_].q; ;for 1..4;$,=~s;^.;;;$,});$;-setup;

___
List: Catalyst@lists.rawmode.org
Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@lists.rawmode.org/
Dev site: http://dev.catalyst.perl.org/


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

2007-01-03 Thread Brian Kirkbride

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

___
List: Catalyst@lists.rawmode.org
Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@lists.rawmode.org/
Dev site: http://dev.catalyst.perl.org/