Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Proper way to drilldown using DBIx::Class?

by ChuckP (Novice)
on Jul 22, 2013 at 21:50 UTC ( #1045739=perlquestion: print w/replies, xml ) Need Help??
ChuckP has asked for the wisdom of the Perl Monks concerning the following question:

First time poster, please forgive any transgressions!
Our company is moving from Class::DBI to DBIx::Class after many years of using the former. Anyway, I've been working my way through the tutorials and have a question about writing a drill down procedure, it was very easy in Class::DBI. Here's a snippet from the beginner's guide:

#!/usr/bin/perl use strict; use Music::Artist; my ($artist) = Music::Artist->search_like(name => '%Beatles%'); print $artist->name,"\n"; foreach ($artist->cds) { print "\t", $_->title, "\n"; foreach my $track ($_->tracks) { print "\t\t", $track->songid->name,"\n"; } }
Here is the code I have working using DBIx::Class:The ## are to show what I've tried and the associated error message.
#!/usr/bin/perl -w use strict; use warnings; use lib qw( /usr/local/ActivePerl-5.16/lib /usr/local/ActivePerl-5.16/site/lib ); use MyDatabase::Main; my $schema = MyDatabase::Main->connect("dbi:mysql:dbname=test", "xxxxxxx", "XXXXXXX"); my $artistname = 'Michael Jackson'; my $rs = $schema->resultset('Artist')->search( { 'name' => ${artistname} }, ## If Join used: DBIx::Class::ResultSource::_resolve_join(): No +such relationship 'Cd' on Artist ## { ## join => { 'Cd' => 'Track' }, ## order_by => 'cd.title' , ## }, ); while (my $artist = $rs->next) { print "Artist: " . $artist->name . "\n"; ## Get: Can't locate object method "cd" via package "MyDatabase: +:Main::Result::Artist" ## print $artist->cd->title . "\n=======================\n"; my $cd_rs = $artist->search_related('cds', { artist => $artist-> +id }); while (my $cd_rec = $cd_rs->next) { print " " . $cd_rec->title . ".\n =======================\n +"; my $track_rs = $cd_rec->search_related('tracks', { cd => $cd_r +ec->cdid }); while ( my $track_rec = $track_rs->next) { print " " . $track_rec->trackid . ".) " . $track_rec->titl +e . ".\n"; } print "\n"; } } print "\n"; print "\n\nFinished.\n\n"; exit;

Is this the best practice for drilling down though DBIx::Class? Or am I using the wrong terminology? I've tried searching both Perlmonks and the internet to no avail.
Much Thanks in Advance!!

Replies are listed 'Best First'.
Re: Proper way to drilldown using DBIx::Class?
by NetWallah (Abbot) on Jul 23, 2013 at 04:24 UTC
    Welcome to the Monastery !

    Have you regenerated your schema using 'dbicdump' after making changes ?

    This is the biggest operatioal change when moving from Class:DBI , where that step is not required.

    FYI - here is a very similar functioning baby app I wrote when learning this stuff (which I have subsequently forgotten):

    use strict; use warnings; use MusicApp::Schema; #MusicApp::Schema::Result::Band->has_many( albums => 'MusicApp::Schema +::Result::Album', 'band');#{ '' => '' }); my $bandname = $ARGV[0] || "The Beatles"; my $DB_NAME="musicdb.sqlite"; my $schema = MusicApp::Schema->connect("dbi:SQLite:$DB_NAME"); my $bandrs = $schema->resultset('Band')->search({ name => { -like => +"\%$bandname\%" } }) ; while (my $b = $bandrs->next){ print "BAND: ",$b->name, " Started ", $b->year,"\n"; my $albumrs = $b->albums; while (my $a = $albumrs->next){ print "\tALBUM: ",$a->year,"\t",$a->name,"\n"; my $songrs = $a->songs; while (my $s = $songrs->next){ print "\t\tSONG: ",$s->track, "\t",$s->name,"\n"; } } }

                 My goal ... to kill off the slow brain cells that are holding me back from synergizing my knowledge of vertically integrated mobile platforms in local cloud-based content management system datafication.

      Howdy NetWallah! Thanks! I actually been lurking around PerlMonks for a long time and usually found what I needed. So I didn't need to post anything, until now. I'm sure I'll be posting again soon! 8uO I had to look up what dbicdump. I just copied and pasted everything from the DBIx::Class::Manual::Example doc. I'm just getting started on learning DBIx:Class, so outside of my one experiment. I have ventured outside of the examples at all. Your code was great!! Here's the code changes:

      use strict; use lib qw( ... ); use MyDatabase::Main; my ($s_seconds, $s_microseconds) = gettimeofday; my $schema = MyDatabase::Main->connect('dbi:mysql:dbname=test', "xxxxxx", "XXXXX"); my $artistname = 'Michael Jackson'; my $artists_rs = $schema->resultset('Artist'); ## my $artists_rs = $schema->resultset('Artist')->search( ## { 'name' => "Micha +el Jackson" } ## ); while (my $a = $artists_rs->next) { print "Artist ID: ", $a->artistid, ".) ", $a->name, ".\n"; my $cd_rs = $a->cds; while (my $c = $cd_rs->next) { print " CD Id: ", $c->cdid, ".) ", $c->title, ".\n"; my $track_rs = $c->tracks; while (my $t = $track_rs->next) { print " ID: ", $t->trackid, ".) ", $t->title, ".\n"; } print "\n"; } ## CD's Loop. print "\n"; } ## Artists Loop. my ($e_seconds, $e_microseconds) = gettimeofday; my $elapsed = tv_interval( [$s_seconds, $s_microseconds], [$e_seconds, $e_microseconds] ); print "Elapsed:${elapsed}.\n";
      ... And the Results:
      Artist ID: 1.) Michael Jackson. CD Id: 1.) Thriller. ID: 3.) Billie Jean. ID: 6.) Beat It. CD Id: 2.) Bad. ID: 4.) Leave Me Alone. ID: 5.) Smooth Criminal. ID: 7.) Dirty Diana. Artist ID: 2.) Eminem. CD Id: 3.) The Marshall Mathers LP. ID: 1.) The Way I Am. ID: 2.) Stan. Artist ID: 3.) Nick Drake. CD Id: 4.) Pink Moon. ID: 9.) From the Morning. ID: 10.) Pink Moon. ID: 14.) Things Behind the Sun. CD Id: 6.) Bryter Layter. ID: 8.) Northern Sky. ID: 13.) Bryter Layter. Artist ID: 4.) The Doors. CD Id: 5.) LA Woman. ID: 11.) Riders on the Storm. ID: 12.) Love Her Madly. Elapsed:0.223685.

      Bless you for your kindness and sharing! PS - Is there a place to check that your response was the correct answer (like Since you should get your creds

        You are quite welcome!

        PerlMonks does not have a "correct Answer" category - everything is based on reputation of the node - You can credit me by upvoting (++) my writeup.

        FYI - I upvoted yours, because you followed up with acknowldgement, and a a final. functioning solution for others to follow.

        Cheers! , and FYI - The Doors are one of my favourites. I was saddened when Ray Manzerek passed away 2 months ago.

                     My goal ... to kill off the slow brain cells that are holding me back from synergizing my knowledge of vertically integrated mobile platforms in local cloud-based content management system datafication.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1045739]
Approved by Perlbotics
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2017-05-28 06:54 GMT
Find Nodes?
    Voting Booth?