[Catalyst] Re: difficulty in assigning an array to a stash

Larry Leszczynski larryl at emailplus.org
Mon Nov 12 15:49:43 GMT 2007


Hi Jagdish -

> 	my @options = $c->model('myleavedb::txn_mdl')->search({emp_no =>
> $c->user->emp_no},{columns => [qw/lv_id/]});

When you do the search via the model, @options will be an array of 
objects of the type defined in "myleavedb::txn_mdl".  Because of your 
"columns" specification, each of those objects will have the "lv_id" 
column/method available.


> 	foreach my $option(@options) {
> 	push (@new_options,$option->lv_id)
> 	}
> 	$c->stash->{myoptions} = \@new_options;
>
> I don't understand in what way @options and @new_options are different
> from each other, but in a bumbling manner I have made the code work.

Your new code loops over the objects, pulls out lv_id values into 
@new_options, and passes \@new_options into the template.

Another way would be to pass \@options directly into the template, and 
in your template you would use something like:

    FOREACH options IN options;
        lv_id = options.lv_id;
        ... do stuff with lv_id ...
    END

Depending on circumstances this may be a better approach in the long 
run because, for example, later you may decide you need additional 
columns from @options, and you would be able to access those in 
your template as option.lv_id, option.some_other_method, etc.


Larry



More information about the Catalyst mailing list