FAQ
Hi,

I have a DBIC record object like
my $obj = $c->model("Database::Table")->find($id);

The table has very many fields and I would like to put their values in a TT
template without inserting them one by one in the stash.

So I would like to create a hash ref from $obj where the name of the field
is the key and the value from the table is the value in that hash, then use
$c->stash($hashref).

Is it possible to create that hash (ref) from $obj, or I need to do
something like

$c->stash->{obj} = $obj;

and in the template use [% obj.name1 %]... [% obj.name2 %]?

And by the way, which do you think is the recommended way?

Thank you.




Octavian

Search Discussions

  • Will Hawes at Mar 13, 2007 at 10:46 am

    On 13/03/07, Octavian Rasnita wrote:
    Hi,

    I have a DBIC record object like
    my $obj = $c->model("Database::Table")->find($id);

    The table has very many fields and I would like to put their values in a TT
    template without inserting them one by one in the stash.

    So I would like to create a hash ref from $obj where the name of the field
    is the key and the value from the table is the value in that hash, then use
    $c->stash($hashref).

    Is it possible to create that hash (ref) from $obj, or I need to do
    something like

    $c->stash->{obj} = $obj;

    and in the template use [% obj.name1 %]... [% obj.name2 %]?

    And by the way, which do you think is the recommended way?
    I'd have thought the second one. $obj is essentially just a hash, so
    I'm not sure what you'd stand to gain using the first method.
  • Simon Wilcox at Mar 13, 2007 at 11:09 am

    On Tue, 13 Mar 2007, Octavian Rasnita wrote:

    I have a DBIC record object like
    my $obj = $c->model("Database::Table")->find($id);

    The table has very many fields and I would like to put their values in a TT
    template without inserting them one by one in the stash.

    So I would like to create a hash ref from $obj where the name of the field
    is the key and the value from the table is the value in that hash, then use
    $c->stash($hashref).
    I'm not sure why you would want to do this ?
    Is it possible to create that hash (ref) from $obj, or I need to do
    something like

    $c->stash->{obj} = $obj;

    and in the template use [% obj.name1 %]... [% obj.name2 %]?
    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.

    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/
  • Octavian Rasnita at Mar 13, 2007 at 11:21 am
    From: "Simon Wilcox" <simonw@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.

    Octavian
  • Daniel McBrearty at Mar 13, 2007 at 11:32 am
    basically : an object IS a (blessed) hash.

    http://perldoc.perl.org/perlboot.html

    You kind of need to understand this, if you don't already. It's worth
    having a read through the tutorials.

    D


    On 3/13/07, Octavian Rasnita wrote:
    From: "Simon Wilcox" <simonw@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.

    Octavian


    _______________________________________________
    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/

    --
    Daniel McBrearty
    email : danielmcbrearty at gmail.com
    www.engoi.com : the multi - language vocab trainer
    BTW : 0873928131
  • Simon Wilcox at Mar 13, 2007 at 12:01 pm

    On Tue, 13 Mar 2007, Daniel McBrearty wrote:

    basically : an object IS a (blessed) hash.
    Not necessarily, you can also bless scalars and arrays. A blessed array,
    in particular can be a very effective way of improving performance for
    certain types of data structures.
    You kind of need to understand this, if you don't already. It's worth
    having a read through the tutorials.
    And buy Damian's book - Object Oriented Perl.

    http://www.amazon.co.uk/Object-Oriented-Perl-Damian-Conway/dp/1884777791

    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/
  • Grün Christian-Rolf at Mar 13, 2007 at 12:15 pm

    Simon Wilcox wrote:
    Not necessarily, you can also bless scalars and arrays. A blessed array,
    in particular can be a very effective way of improving performance for
    certain types of data structures.
    Strictly speaking you can bless any reference, although the most useful
    are hashes and arrays.
  • Daniel McBrearty at Mar 13, 2007 at 12:18 pm

    On 3/13/07, Kiki wrote:
    Simon Wilcox wrote:
    Not necessarily, you can also bless scalars and arrays. A blessed array,
    in particular can be a very effective way of improving performance for
    certain types of data structures.
    Strictly speaking you can bless any reference, although the most useful
    are hashes and arrays.
    true. So it's perhaps more accurate to say "you can put a ref to
    anything ($@%, blessed or not ... ) on the stash."

    --
    Daniel McBrearty
    email : danielmcbrearty at gmail.com
    www.engoi.com : the multi - language vocab trainer
    BTW : 0873928131
  • Octavian Rasnita at Mar 13, 2007 at 12:18 pm
    I know that an object is a blessed hash, but the DBIC objects are very
    complex, and I cannot use

    $c->stash($obj);

    If I do that, the values from $obj hash reference are not put in the
    template like when $obj is a reference to a common hash.

    That's why I want to find how to put the key/values from $obj into a common
    hash.

    Is it possible to do that without specifying all the keys by name? Or at
    least is there a way to get all the keys from $obj, then loop and create a
    hash, something like:

    my $hash;
    foreach(@keys) {
    $hash->{$_} = $obj->$_;
    }

    Thanks.

    Octavian
  • Daniel McBrearty at Mar 13, 2007 at 12:31 pm

    If I do that, the values from $obj hash reference are not put in the
    template like when $obj is a reference to a common hash.
    Impossible to know what you mean here without an example of the
    template, but I commonly put DBIC objects on the stash, and call
    methods on them with the dot operator in TT. TT is smart enough to
    work out what needs to be done and do it

    so whether the underlying code is

    $obj->element;

    or

    $obj->{element};

    [%- obj.element -%]

    works. (Maybe it always uses the second version - as I've never had a
    problem, I've never looked ...

    So I don't really get what the problem is here.

    --
    Daniel McBrearty
    email : danielmcbrearty at gmail.com
    www.engoi.com : the multi - language vocab trainer
    BTW : 0873928131
  • Octavian Rasnita at Mar 13, 2007 at 1:15 pm
    From: "Daniel McBrearty" <danielmcbrearty@gmail.com>
    Impossible to know what you mean here without an example of the
    template, but I commonly put DBIC objects on the stash, and call
    methods on them with the dot operator in TT. TT is smart enough to
    work out what needs to be done and do it

    so whether the underlying code is

    $obj->element;

    or

    $obj->{element};

    [%- obj.element -%]

    works. (Maybe it always uses the second version - as I've never had a
    problem, I've never looked ...

    So I don't really get what the problem is here.
    Hi,

    The problem appears when I want to use only [% element %] and not [%
    obj.element %] in templates.
    And I want to use the first way because there are very many variables and it
    is more simple.

    Octavian
  • Daniel McBrearty at Mar 13, 2007 at 1:28 pm

    Hi,

    The problem appears when I want to use only [% element %] and not [%
    obj.element %] in templates.
    And I want to use the first way because there are very many variables and it
    is more simple.
    As others have said, I think this is going to bite you in the arse
    later. K-I-S-S. It seems we have differing definitions of "simple"
    though.



    --
    Daniel McBrearty
    email : danielmcbrearty at gmail.com
    www.engoi.com : the multi - language vocab trainer
    BTW : 0873928131
  • Simon Wilcox at Mar 13, 2007 at 12:33 pm

    On Tue, 13 Mar 2007, Octavian Rasnita wrote:

    That's why I want to find how to put the key/values from $obj into a common
    hash.
    It sounds like this might be a bad design decision. Why would you not want
    to group your template variables ?

    As your app grows you'd be much more likely to see one key trample on
    another from a different object.
    Is it possible to do that without specifying all the keys by name? Or at
    least is there a way to get all the keys from $obj, then loop and create a
    hash, something like:

    my $hash;
    foreach(@keys) {
    $hash->{$_} = $obj->$_;
    }
    Yes, see my earlier post. You can get the columns from the ResultSet
    object.

    S.

    --
    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/
  • Carl Johnstone at Mar 13, 2007 at 12:56 pm

    Is it possible to do that without specifying all the keys by name? Or at
    least is there a way to get all the keys from $obj, then loop and create a
    hash, something like:

    my $hash;
    foreach(@keys) {
    $hash->{$_} = $obj->$_;
    }

    You're asking for trouble with something like that. Create a DB column
    called "template" and BOOM!

    You're almost certainly better off doing this on the template side of
    things:

    [% FOREACH obj %]
    Name: [% name %]
    [% END %]

    Will do the same as:

    Name: [% obj.name %]

    as long as there is only one obj.

    Carl
  • Dave Howorth at Mar 13, 2007 at 12:44 pm

    Daniel McBrearty wrote:
    basically : an object IS a (blessed) hash.

    http://perldoc.perl.org/perlboot.html

    You kind of need to understand this, if you don't already. It's worth
    having a read through the tutorials.
    As others have pointed out, objects can also be created by blessing
    other references.

    In the context of the original question, it's also worth remembering
    that an object is not the same as a hash as far as TT is concerned. It
    unifies the syntax to call accessors and to access the members of a
    hash. But it does *not* let you access arbitrary elements in a blessed
    hash as Perl does. In TT, you can only use the methods.

    Which is generally a Good Thing, but occasionally can be surprising.

    Cheers, Dave
  • Dave Rolsky at Mar 13, 2007 at 3:23 pm

    On Tue, 13 Mar 2007, Dave Howorth wrote:

    In the context of the original question, it's also worth remembering
    that an object is not the same as a hash as far as TT is concerned. It
    unifies the syntax to call accessors and to access the members of a
    hash. But it does *not* let you access arbitrary elements in a blessed
    hash as Perl does. In TT, you can only use the methods.
    Huh? That's not true at all. In fact, TT2 does exactly the opposite. If I
    do this in my template:

    [% object.something %]

    It tries to call a method something() on that object. However, if that
    method does not exist and the object is a blessed hash, then TT2
    "helpfully" looks up "something" as a hash key. Of course, if that key
    does not exist, TT2 will "helpfully" just keep going without a warning or
    error. This helpful behavior has cost me many hours of debugging.

    And yes, I know there's a mode for TT2 where you can make it barf on any
    instance of undef in a template substitution, but that's often _too_
    strict.


    -dave

    /*===================================================
    VegGuide.Org www.BookIRead.com
    Your guide to all that's veg. My book blog
    ===================================================*/
  • Simon Wilcox at Mar 13, 2007 at 12:12 pm

    On Tue, 13 Mar 2007, Octavian Rasnita wrote:

    From: "Simon Wilcox" <simonw@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/
  • Octavian Rasnita at Mar 13, 2007 at 12:42 pm
    From: "Simon Wilcox" <simonw@digitalcraftsmen.net>
    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;
    }
    Ok, thank you. This is what I needed.
    You may need to adjust some of the above to get it to recognise $column as
    an accessor.
    I hope I won't have any problem, or I think I can use
    $obj->get_column($column).

    Thank you all.

    Octavian
  • Octavian Rasnita at Mar 13, 2007 at 1:06 pm
    From: "Simon Wilcox" <simonw@digitalcraftsmen.net>
    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 have tried that, but it gave the following error:

    Can't locate object method "columns" via package "DBIx::Class::ResultSet

    What am I doing wrong?

    Thanks.

    Octavian
  • Bogdan Lucaciu at Mar 13, 2007 at 2:27 pm

    On Tuesday 13 March 2007 15:06, Octavian Rasnita wrote:
    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 have tried that, but it gave the following error:

    Can't locate object method "columns" via package "DBIx::Class::ResultSet

    What am I doing wrong?

    it's $resultset->result_source->columns

    --
    Bogdan Lucaciu
    http://www.wiz.ro
  • Matt Lawrence at Mar 13, 2007 at 4:24 pm

    Bogdan Lucaciu wrote:
    On Tuesday 13 March 2007 15:06, Octavian Rasnita wrote:

    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 have tried that, but it gave the following error:

    Can't locate object method "columns" via package "DBIx::Class::ResultSet

    What am I doing wrong?

    it's $resultset->result_source->columns
    Alternatively, get_columns will return a hash (not a reference!) of the
    current row.

    $c->stash->{obj }= { $row->get_columns };

    Note that this hash contains uninflated values, if you want column
    inflation you'll need to loop.

    Matt
  • Octavian Rasnita at Mar 13, 2007 at 5:28 pm
    From: "Matt Lawrence" <matt.lawrence@ymogen.net>
    Alternatively, get_columns will return a hash (not a reference!) of the
    current row.

    $c->stash->{obj }= { $row->get_columns };
    Oh thanks. Finally I've used
    $c->stash->{obj} = $obj;
    and I've modified the template, because it seems that it is a better design.

    What I thought that I need was something like:
    $c->stash({ $row->get_columns });

    And I'm happy that I also found about this method.
    Note that this hash contains uninflated values, if you want column
    inflation you'll need to loop.
    Please tell me what it means column inflation.

    Thanks.

    Octavian
  • Eden Cardim at Mar 13, 2007 at 5:43 pm

    On 3/13/07, Octavian Rasnita wrote:
    Please tell me what it means column inflation.
    Check the docs for DBIx::Class::InflateColumn

    --
    Eden Cardim
    Instituto Baiano de Biotecnologia
    N?cleo de Biologia Computacional e Gest?o de Informa??es Biotecnol?gicas
    Laborat?rio de Bioinform?tica
    --
    "you seem to think that 'close enough' is close enough...
    please learn to be 'literal' around programming."
    merlyn - on irc.freenode.net#perl
  • Matt Lawrence at Mar 13, 2007 at 6:01 pm

    Eden Cardim wrote:
    On 3/13/07, Octavian Rasnita wrote:
    Please tell me what it means column inflation.
    Check the docs for DBIx::Class::InflateColumn
    Short answer: none of the values returned by get_columns will be
    objects, even if they would have been if you had called the named accessor.

    Matt
  • Christopher H. Laco at Mar 13, 2007 at 6:06 pm

    Matt Lawrence wrote:
    Eden Cardim wrote:
    On 3/13/07, Octavian Rasnita wrote:
    Please tell me what it means column inflation.
    Check the docs for DBIx::Class::InflateColumn
    Short answer: none of the values returned by get_columns will be
    objects, even if they would have been if you had called the named accessor.

    Matt
    return map {$_ => $self->$_} $self->columns;

    is your friend. :-)

    -------------- next part --------------
    A non-text attachment was scrubbed...
    Name: signature.asc
    Type: application/pgp-signature
    Size: 189 bytes
    Desc: OpenPGP digital signature
    Url : http://lists.scsys.co.uk/pipermail/catalyst/attachments/20070313/6eb96923/signature.pgp

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupcatalyst @
categoriescatalyst, perl
postedMar 13, '07 at 10:37a
activeMar 13, '07 at 6:06p
posts25
users12
websitecatalystframework.org
irc#catalyst

People

Translate

site design / logo © 2022 Grokbase