Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

xsawyerx (8978)

xsawyerx
  (email not shown publicly)

Journal of xsawyerx (8978)

Tuesday December 16, 2008
04:40 AM

When many-to-many don't work

[ #38093 ]
Recently while working on a Catalyst application, I had trouble with the many-to-many relation of two tables. I used an intermediate table with two one-to-many relations to it and had hoped that DBIC Schema Loader will be able to get it worked out.
Apparently it didn't, and I'm guessing I had something to do with it.

The correct thing would have been to find the error and fix it, but I can honestly say I've opted for the lesser path, which is to use the intermediate table directly and write the wrapper code to make sure there aren't any conflicts. It's times like these that DBIC's find_or_new() and $obj->in_storage are helpful - and even more so for writing the proper message to the user.

Here is a snippet I used for the intermediate table:
my @borrows = $c->model( 'DB::Borrowings')->find( { subscriber_id => $id } );
push @{ $c->stash->{'borrowed_titles'} },
  map {
    $c->model( 'DB::Titles' )->find( { id => $_->id } )
  } @borrows;

I read the [intermediate] Borrowings table and fetch all the IDs for Borrowed titles by this specific subscriber into a list. Then I go over that list and fetch the Titles that have those IDs.Then I push them all into a variable I could use in Template Toolkit.
Oh, and it's actually only two lines. Perl is just simplicity at the palm of my hands.

It's a workaround, but it works. :)

Note to self: next time fix the SQL relations and choose a better name for an intermediate table.
The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.