[Catalyst] putting an object in the stash

Simon Wilcox simonw at digitalcraftsmen.net
Tue Mar 13 12:12:44 GMT 2007


On Tue, 13 Mar 2007, Octavian Rasnita wrote:

> From: "Simon Wilcox" <simonw at digitalcraftsmen.net>
>
> > I just put the object in the stash. TT abstracts the method/hash key
> > accessor issue for you so that [% obj.name1 %] will work whether obj is an
> > object with a name1 accessor or a hash with a name1 key.
> >
>
> Ok, then I will use that way. I hoped that the first solution could be
> possible, because I don't want to use [% obj.name %] but only [% name %] if
> it works.

Ah, I didn't understand that from reading your original post.

On Tue, 13 Mar 2007, Octavian Rasnita wrote:
>
> I have a DBIC record object like
> my $obj = $c->model("Database::Table")->find($id);

You will have to iterate over the accessors individually to put them into
the stash (I think) but you should be able to use DBIC to do most of the
thinking.  Something like (untested):

my $model = $c->model("Database::Table");
foreach my $column (@{$model->columns}) {
    $column =~ s{me\.}{}; # strip the prefix DBIC adds
    $c->stash->{$column} = $obj->$column;
}

I got the idea from reading the docs:
http://search.cpan.org/~jrobinson/DBIx-Class-0.07005/lib/DBIx/Class/ResultSet.pm#columns

You may need to adjust some of the above to get it to recognise $column as
an accessor.

Simon Wilcox

-- 
Digital Craftsmen Ltd
Exmouth House, 3 Pine Street, London. EC1R 0JH
t 020 7183 1410 f 020 7099 5140 m 07951 758698
w http://www.digitalcraftsmen.net/



More information about the Catalyst mailing list