Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Accessing many-to-many relationships in Catalyst

by Your Mother (Archbishop)
on Aug 24, 2014 at 02:06 UTC ( [id://1098438]=note: print w/replies, xml ) Need Help??


in reply to Accessing many-to-many relationships in Catalyst

We would have to see your relationships to help, I think. Also this does not sound like a many_to_many situation to me. A book has_many chapters and has_many pages, as does the chapter. They each belong_to a chapter and a book. Many pages are not in many books.

Pseudo-ish code, three Result files: Book, Chapter, Page

package Book; use parent "DBIx::Class::Core"; __PACKAGE__->table("book"); __PACKAGE__->add_columns(qw/ id title /); __PACKAGE__->set_primary_key("id"); __PACKAGE__->has_many( chapters => "Chapter", { "foreign.book" => "self.id" } ); # Probably a belongs_to Author here... 1;
package Chapter; use parent "DBIx::Class::Core"; __PACKAGE__->table("chapter"); __PACKAGE__->add_columns(qw/ id title num book /); __PACKAGE__->set_primary_key("id"); __PACKAGE__->has_many( pages => "Page", { "foreign.chapter" => "self.id" } ); __PACKAGE__->belongs_to( book => "Book" ); 1;
package Page; use parent "DBIx::Class::Core"; __PACKAGE__->table("page"); __PACKAGE__->add_columns(qw/ id content chapter num /); __PACKAGE__->set_primary_key("id"); __PACKAGE__->belongs_to( book => "Chapter" ); 1;

Then for TT2, [% page.chapter.book.title %]: [% page.chapter.num %]. And–

# Gets latest 10 results while ( my $page = $rs->next ) { my $book = sprintf( "%02d", $page->chapter->book->title ); # Bo +ok number??? my $chapter = sprintf( "%02d", $page->chapter->num ); }

Note, I feel "position" or "order" (bad name for DB columns) is superior to "num." It is self-regulating if set-up right, like an array, if you remove an element (page or chapter) from the middle, all the others adjust.

Replies are listed 'Best First'.
Re^2: Accessing many-to-many relationships in Catalyst
by LunarCowgirl (Sexton) on Aug 25, 2014 at 02:57 UTC

    Thanks for the advice and the examples you gave. They were very helpful, and perhaps I may make a few changes in the database design before the site goes into production. The relationship I have is indeed many-to-many but could probably be expressed better like you suggested. However, I did manage to solve my problem. Your post caused me to look up a few things, and I learned what I was doing wrong.

    In a many-to-many relationship, if you're trying to get a single value, you need to use first->, like so:

    $page->chapter->first->num

    That does what I needed. Apparently Template Toolkit doesn't require that, as I haven't had to use first with it to do the same thing.

    Again. Thanks for your help. :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1098438]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-03-29 00:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found