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.
But I could have defined a Schema manually
xsawyerx on 2008-12-16T14:10:36
Although it's good to know that this is simply something Loader is probably not able to do.Re:But I could have defined a Schema manually
oliver on 2008-12-16T14:20:06
Yes I usually start with Schema::Loader and then go in below the MD5 sum line and add the many to many relation by hand. This means if you regenerate the Schema it should preserve your added code, just rewriting the part above the MD5 sum. (sorry if you already knew this:-) I totally forgot
xsawyerx on 2008-12-16T14:41:46
That's a great idea.
Thanks!