[Dbix-class] get_columns and overloaded column accessors

Francesc Romà i Frigolé francesc.roma+dbix at gmail.com
Wed Oct 20 16:17:29 GMT 2010


On Wed, Oct 20, 2010 at 2:57 PM, Peter Rabbitson
<rabbit+dbic at rabbit.us<rabbit%2Bdbic at rabbit.us>
> wrote:

> Francesc Rom=E0 i Frigol=E9 wrote:
>
>> Hello,
>>
>> I'm using overloaded column accessors as described in the Cookbok <
>> http://search.cpan.org/%7Efrew/DBIx-Class-0.08123/lib/DBIx/Class/Manual/=
Cookbook.pod#OVERLOADING_METHODS>.
>> For a given row, I'd like to get a hash with all the column names/value
>> pairs like Row->get_columns returns. However, get_columns returns the ac=
tual
>> column values, not the ones created with the custom overloaded accessors.
>> get_inflated_columns behaves the same way.
>>
>
> I don't believe you. Here is the implementation of get_inflated_columns
> from the current CPAN version:
>
>
Thanks Peter for the prompt reply and your explanations. I believe you, but
I upgraded to the current CPAN version, and still see the same result with
get_inflated_columns, also the same happens in a different computer with an
older version (both running different versions of ubuntu). It's very weird,
but I haven't yet traced the issue any further, so I can't give you any more
insight.



>
>  Is there a better way of doing this? The reason why I want the hash is to
>> pass the data to my view in a Catalyst application.
>>
>
> Use
> http://search.cpan.org/~frew/DBIx-Class-0.08123/lib/DBIx/Class/ResultClas=
s/HashRefInflator.pm<http://search.cpan.org/%7Efrew/DBIx-Class-0.08123/lib/=
DBIx/Class/ResultClass/HashRefInflator.pm>
>
>
>
HashRefInflator does indeed put the values given by the overloaded
accessors, it even expands the values corresponding to the related objects
that I have prefetched. This is *almost* what I want, I'm just missing the
ability to be able to call methods on the related objects that don't
correspond to actual columns, ex: $track_row->cd->fancy_formatted_title

I might be missing something here, but it seems to me that I can't use
HashRefInflator for a specific row, I have to use it for a result set. If I
could use something like HashRefInflator for a row, I could first call the
methods I need and then convert it into a hashref.

It's not a big deal, I can just create the hash myself, I'm just concerned I
might be doing something wrong.




>  BTW, the documentation of Row->get_column says "Returns a raw column val=
ue
>> from the row object, if it has already been fetched from the database or=
 set
>> by an accessor." Which made me think it would do what I need, but it
>> doesn't.
>>
>
> Yes, it returns the raw value, regardless of what transformations took
> place, it reaches into the object itself. Please suggest better wording
> for above piece of documentation (or just send a github pull req :)
>

Well, now that I'm reading it again it seems more clear :)



>
>  To illustrate my point, I took the code in DBIx::Class::Manual::Example,
>> and added the following accessor in the result Track:
>>
>> ...
>>
>>
>> As you can see, the overloaded accessor works with the row object, but is
>> not used by get_columns.
>>
>
> Yes, expected and by design.
>
>

Fine, but I get the same result with get_inflated_results

  while (my $track =3D $rs->next) {
    print $track->title . "\n";
    my $track_href =3D { $track->get_inflated_columns };
    print "inflated: " . $track_href->{title} . "\n";
  }

---

Leave Me Alone -> my favourite song!
inflated: Leave Me Alone
Smooth Criminal -> my favourite song!
inflated: Smooth Criminal
Dirty Diana -> my favourite song!
inflated: Dirty Diana


I know you won't believe it :p It's ok, I'm actually not interested in the
inflated values, I'm just mentioning it for completeness, I need the hash
for a JSON view.

Francesc
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/dbix-class/attachments/20101020/b8b=
b250a/attachment.htm


More information about the DBIx-Class mailing list