Catalyst-Manual

 view release on metacpan or  search on metacpan

lib/Catalyst/Manual/Tutorial/03_MoreCatalystBasics.pod  view on Meta::CPAN

        padding: 0 0 50% 5px;
        font-weight: normal;
        background-color: #ddd;
        width: 100px;
    }
    #content {
        margin-left: 120px;
    }
    .message {
        color: #390;
    }
    .error {
        color: #f00;
    }

You may wish to check out a "CSS Framework" like Emastic
(L<http://code.google.com/p/emastic/>) as a way to quickly provide lots
of high-quality CSS functionality.


=head2 Test Run The Application

Hit "Reload" in your web browser and you should now see a formatted
version of our basic book list. (Again, the development server should
have automatically restarted when you made changes to
F<lib/MyApp/View/HTML.pm>. If you are not using the "-r" option, you
will need to hit C<Ctrl-C> and manually restart it. Also note that the
development server does I<NOT> need to restart for changes to the TT and
static files we created and edited in the C<root> directory -- those
updates are handled on a per-request basis.)

Although our wrapper and stylesheet are obviously very simple, you
should see how it allows us to control the overall look of an entire
website from two central files. To add new pages to the site, just
provide a template that fills in the C<content> section of our wrapper
template -- the wrapper will provide the overall feel of the page.


=head2 Updating the Generated DBIx::Class Result Class Files

If you take a look at the Schema files automatically generated by
L<DBIx::Class::Schema::Loader>, you will see that it has already defined
C<has_many> and C<belongs_to> relationships on each side of our foreign
keys. For example, take a look at F<lib/MyApp/Schema/Result/Book.pm> and
notice the following code:

    =head1 RELATIONS

    =head2 book_authors

    Type: has_many

    Related object: L<MyApp::Schema::Result::BookAuthor>

    =cut

    __PACKAGE__->has_many(
      "book_authors",
      "MyApp::Schema::Result::BookAuthor",
      { "foreign.book_id" => "self.id" },
      { cascade_copy => 0, cascade_delete => 0 },
    );

Each C<Book> "has_many" C<book_authors>, where C<BookAuthor> is the
many-to-many table that allows each Book to have multiple Authors, and
each Author to have multiple books.  The arguments to C<has_many> are:

=over 4

=item *

C<book_authors> - The name for this relationship.  DBIC will create an
accessor on the C<Books> DBIC Row object with this name.

=item *

C<MyApp::Schema::Result::BookAuthor> - The name of the DBIC model class
referenced by this C<has_many> relationship.

=item *

C<foreign.book_id> - C<book_id> is the name of the foreign key column in
the I<foreign> table that points back to this table.

=item *

C<self.id> - C<id> is the name of the column in I<this> table that is
referenced by the foreign key.

=back

See L<DBIx::Class::Relationship/has_many> for additional information.
Note that you might see a "hand coded" version of the C<has_many>
relationship above expressed as:

    __PACKAGE__->has_many(
      "book_authors",
      "MyApp::Schema::Result::BookAuthor",
      "book_id",
    );

Where the third argument is simply the name of the column in the foreign
table.  However, the hashref syntax used by
L<DBIx::Class::Schema::Loader> is more flexible (for example, it can
handle "multi-column foreign keys").

B<Note:> If you are using older versions of SQLite and related DBIC
tools, you will need to manually define your C<has_many> and
C<belongs_to> relationships. We recommend upgrading to the versions
specified above. :-)

Have a look at F<lib/MyApp/Schema/Result/BookAuthor.pm> and notice that
there is a C<belongs_to> relationship defined that acts as the "mirror
image" to the C<has_many> relationship we just looked at above:

    =head1 RELATIONS

    =head2 book

    Type: belongs_to



( run in 2.028 seconds using v1.01-cache-2.11-cpan-f56aa216473 )