Dear Monks,

The following is from the documentation at DBIx::Class::ResultSet which describes a populate method that can take an entire hash structure to populate related tables:

$schema->resultset("Artist")->populate([ { artistid => 4, name => 'Manufactured Crap', cds => [ { title => 'My First CD', year => 2006 }, { title => 'Yet More Tweeny-Pop crap', year => 2007 }, ], }, { artistid => 5, name => 'Angsty-Whiny Girl', cds => [ { title => 'My parents sold me to a record company', year => 200 +5 }, { title => 'Why Am I So Ugly?', year => 2006 }, { title => 'I Got Surgery and am now Popular', year => 2007 } ], }, ]);

I am basically looking for the inverse action of populate that instantly gives me a Perl hash with exactly 'that' format with the results from the database. In the following code I'll try to illustrate this with the method to_data that is a fictional method to demonstrate what I would like to achieve:

my $artistsAndTitles = $schema->resultset('Artist')-> search(...)->search_related_rs('cds', {...})->as_data # or another case using a accessor-method with the name 'cds' from a m +any_to_many relation: my $artistsAndTitles = $schema->resultset('Artist')-> cds()->as_data ;

The expected result that I hope for in $artistsAndTitles is a Perl array/hash construct like:

[ { artistid => 4, name => 'Manufactured Crap', cds => [ { title => 'My First CD', year => 2006 }, { title => 'Yet More Tweeny-Pop crap', year => 2007 }, ], }, { artistid => 5, name => 'Angsty-Whiny Girl', cds => [ { title => 'My parents sold me to a record company', year => 200 +5 }, { title => 'Why Am I So Ugly?', year => 2006 }, { title => 'I Got Surgery and am now Popular', year => 2007 } ], } ]

I done quite a bit of reading regards DBIx::Class but can't find a method to do this. Is there any method 'out-of-the-box' that can do this (correctly, taken different types of relations in account such as one-to-many or many-to-many)?

With best regards,

Veltro


In reply to DBIx::Class::ResultSet desired hash output including related items by Veltro

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.