Catalyst-Manual
view release on metacpan or search on metacpan
lib/Catalyst/Manual/Tutorial/03_MoreCatalystBasics.pod view on Meta::CPAN
Open F<lib/MyApp/Controller/Books.pm> and un-comment the model code we
left disabled earlier so that your version matches the following
(un-comment the line containing C<< [$c->model('DB::Book')->all] >>
and delete the next 2 lines):
=head2 list
Fetch all book objects and pass to books/list.tt2 in stash to be displayed
=cut
sub list :Local {
# Retrieve the usual Perl OO '$self' for this object. $c is the Catalyst
# 'Context' that's used to 'glue together' the various components
# that make up the application
my ($self, $c) = @_;
# Retrieve all of the book records as book model objects and store
# in the stash where they can be accessed by the TT template
$c->stash(books => [$c->model('DB::Book')->all]);
# Set the TT template to use. You will almost always want to do this
# in your action methods (action methods respond to user input in
# your controllers).
$c->stash(template => 'books/list.tt2');
}
B<TIP>: You may see the C<< $c->model('DB::Book') >> un-commented above
written as C<< $c->model('DB')->resultset('Book') >>. The two are
equivalent. Either way, C<< $c->model >> returns a
L<DBIx::Class::ResultSet> which handles queries
against the database and iterating over the set of results that is
returned.
We are using the C<< ->all >> to fetch all of the books. DBIC supports
a wide variety of more advanced operations to easily do things like
filtering and sorting the results. For example, the following could be
used to sort the results by descending title:
$c->model('DB::Book')->search({}, {order_by => 'title DESC'});
Some other examples are provided in
L<DBIx::Class::Manual::Cookbook/Complex WHERE clauses>, with additional
information found at L<DBIx::Class::ResultSet/search>,
L<DBIx::Class::Manual::FAQ/Searching>, L<DBIx::Class::Manual::Intro> and
L<Catalyst::Model::DBIC::Schema>.
=head2 Test Run The Application
First, let's enable an environment variable that causes L<DBIx::Class>
to dump the SQL statements used to access the database. This is a
helpful trick when you are trying to debug your database-oriented code.
Press C<Ctrl-C> to break out of the development server and enter:
$ export DBIC_TRACE=1
$ script/myapp_server.pl -r
This assumes you are using bash as your shell -- adjust accordingly if
you are using a different shell (for example, under tcsh, use
C<setenv DBIC_TRACE 1>).
B<NOTE:> You can also set this in your code using
C<< $class->storage->debug(1); >>. See
L<DBIx::Class::Manual::Troubleshooting> for details (including options
to log to a file instead of displaying to the Catalyst development
server log).
Then launch the Catalyst development server. The log output should
display something like:
$ script/myapp_server.pl -r
[debug] Debug messages enabled
[debug] Statistics enabled
[debug] Loaded plugins:
.----------------------------------------------------------------------------.
| Catalyst::Plugin::ConfigLoader 0.30 |
| Catalyst::Plugin::StackTrace 0.11 |
'----------------------------------------------------------------------------'
[debug] Loaded dispatcher "Catalyst::Dispatcher"
[debug] Loaded engine "Catalyst::Engine"
[debug] Found home "/home/catalyst/MyApp"
[debug] Loaded Config "/home/catalyst/MyApp/myapp.conf"
[debug] Loaded components:
.-----------------------------------------------------------------+----------.
| Class | Type |
+-----------------------------------------------------------------+----------+
| MyApp::Controller::Books | instance |
| MyApp::Controller::Root | instance |
| MyApp::Model::DB | instance |
| MyApp::Model::DB::Author | class |
| MyApp::Model::DB::Book | class |
| MyApp::Model::DB::BookAuthor | class |
| MyApp::View::HTML | instance |
'-----------------------------------------------------------------+----------'
[debug] Loaded Private actions:
.----------------------+--------------------------------------+--------------.
| Private | Class | Method |
+----------------------+--------------------------------------+--------------+
| /default | MyApp::Controller::Root | default |
| /end | MyApp::Controller::Root | end |
| /index | MyApp::Controller::Root | index |
| /books/index | MyApp::Controller::Books | index |
| /books/list | MyApp::Controller::Books | list |
'----------------------+--------------------------------------+--------------'
[debug] Loaded Path actions:
.-------------------------------------+--------------------------------------.
| Path | Private |
+-------------------------------------+--------------------------------------+
| / | /default |
| / | /index |
| /books | /books/index |
| /books/list | /books/list |
'-------------------------------------+--------------------------------------'
[info] MyApp powered by Catalyst 5.80020
HTTP::Server::PSGI: Accepting connections at http://0:3000
( run in 1.779 second using v1.01-cache-2.11-cpan-75ffa21a3d4 )