Update year on copyright
[catagits/Catalyst-Manual.git] / lib / Catalyst / Manual / Tutorial / 03_MoreCatalystBasics.pod
index 1f66bac..b8ef611 100644 (file)
@@ -102,12 +102,14 @@ the command if you are using Strawberry Perl.)
 
 =head1 EDIT THE LIST OF CATALYST PLUGINS
 
-One of the greatest benefits of Catalyst is that it has such a large
-library of plugins and base classes available.  Plugins are used to
-seamlessly integrate existing Perl modules into the overall Catalyst
-framework.  In general, they do this by adding additional methods to the
-C<context> object (generally written as C<$c>) that Catalyst passes to
-every component throughout the framework.
+One of the greatest benefits of Catalyst is that it has such a large 
+library of bases classes and plugins available that you can use easily 
+add functionality to your application. Plugins are used to seamlessly 
+integrate existing Perl modules into the overall Catalyst framework. In 
+general, they do this by adding additional methods to the C<context> 
+object (generally written as C<$c>) that Catalyst passes to every 
+component throughout the framework. 
+
 
 By default, Catalyst enables three plugins/flags:
 
@@ -226,7 +228,7 @@ the following:
 Don't let these variations confuse you -- they all accomplish the same 
 result.
 
-This tells Catalyst to start using one new plugin, 
+This tells Catalyst to start using one additional plugin, 
 L<Catalyst::Plugin::StackTrace|Catalyst::Plugin::StackTrace>, to add a 
 stack trace to the standard Catalyst "debug screen" (the screen 
 Catalyst sends to your browser when an error occurs). Be aware that 
@@ -293,7 +295,7 @@ and add the following method to the controller:
     
     =cut
     
-    sub list : Local {
+    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
@@ -301,14 +303,14 @@ and add the following method to the controller:
     
         # 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];
+        # $c->stash(books => [$c->model('DB::Book')->all]);
         # But, for now, use this code until we create the model later
-        $c->stash->{books} = '';
+        $c->stash(books => '');
     
         # 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';
+        $c->stash(template => 'books/list.tt2');
     }
 
 B<TIP>: See Appendix 1 for tips on removing the leading spaces when
@@ -323,7 +325,7 @@ is used to pass information between components and provide access to
 Catalyst and plugin functionality.
 
 Catalyst actions are regular Perl methods, but they make use of 
-attributes (the "C<: Local>" next to the "C<sub list>" in the code 
+attributes (the "C<:Local>" next to the "C<sub list>" in the code 
 above) to provide additional information to the Catalyst dispatcher 
 logic (note that the space between the colon and the attribute name is 
 optional; you will see attributes written both ways).  Most Catalyst 
@@ -434,22 +436,20 @@ but its use is now deprecated.
 Enter the following command to enable the C<TT> style of view
 rendering for this tutorial:
 
-    $ script/myapp_create.pl view TT TT
+    $ script/myapp_create.pl view HTML TT
      exists "/home/me/MyApp/script/../lib/MyApp/View"
      exists "/home/me/MyApp/script/../t"
-     created "/home/me/MyApp/script/../lib/MyApp/View/TT.pm"
-     created "/home/me/MyApp/script/../t/view_TT.t"
-
-This simply creates a view called C<TT> (the second 'TT' argument) in 
-a file called C<TT.pm> (the first 'TT' argument). It is now up to you 
-to decide how you want to structure your view layout.  For the 
-tutorial, we will start with a very simple TT template to initially 
-demonstrate the concepts, but quickly migrate to a more typical 
-"wrapper page" type of configuration (where the "wrapper" controls the 
-overall "look and feel" of your site from a single file or set of 
-files).
-
-Edit C<lib/MyApp/View/TT.pm> and you should see that the default
+     created "/home/me/MyApp/script/../lib/MyApp/View/HTML.pm"
+     created "/home/me/MyApp/script/../t/view_HTML.t"
+
+This simply creates a view called C<HTML> in a file called C<HTML.pm> (the first
+argument). It is now up to you to decide how you want to structure your view
+layout.  For the tutorial, we will start with a very simple TT template to
+initially demonstrate the concepts, but quickly migrate to a more typical
+"wrapper page" type of configuration (where the "wrapper" controls the overall
+"look and feel" of your site from a single file or set of files).
+
+Edit C<lib/MyApp/View/HTML.pm> and you should see that the default
 contents contains something similar to the following:
 
     __PACKAGE__->config(TEMPLATE_EXTENSION => '.tt');
@@ -543,7 +543,7 @@ rightfully belongs in a model class.
 
 To test your work so far, first start the development server:
 
-    $ script/myapp_server.pl
+    $ script/myapp_server.pl -r
 
 Then point your browser to L<http://localhost:3000> and you should
 still get the Catalyst welcome page.  Next, change the URL in your
@@ -570,6 +570,7 @@ C<myapp01.sql> in your editor and enter:
     --
     -- Create a very simple database to hold book and author information
     --
+    PRAGMA foreign_keys = ON;
     CREATE TABLE book (
             id          INTEGER PRIMARY KEY,
             title       TEXT ,
@@ -624,8 +625,9 @@ can use the SQLite command line environment to do a quick dump of the
 database contents:
 
     $ sqlite3 myapp.db
-    SQLite version 3.5.9
+    SQLite version 3.6.22
     Enter ".help" for instructions
+    Enter SQL statements terminated with a ";"
     sqlite> select * from book;
     1|CCSP SNRS Exam Certification Guide|5
     2|TCP/IP Illustrated, Volume 1|5
@@ -651,7 +653,7 @@ required if you do a single SQL statement on the command line).  Use
 your OS command prompt.
 
 Please note that here we have chosen to use 'singular' table names. This is
-because the default inflection code for older versions
+because the default inflection code for older versions of
 L<DBIx::Class::Schema::Loader> does NOT handle plurals. There has been much
 philosophical discussion on whether table names should be plural or singular.
 There is no one correct answer, as long as one makes a choice and remains
@@ -688,28 +690,37 @@ framework, a technique that we see in Chapter 4).
 =head2 Make Sure You Have a Recent Version of the DBIx::Class Model
 
 First, let's be sure we have a recent version of the DBIC helper,
-L<Catalyst::Model::DBIC::Schema|Catalyst::Model::DBIC::Schema>, by
-running this command:
+L<Catalyst::Model::DBIC::Schema|Catalyst::Model::DBIC::Schema>, so
+that we can take advantage of some recent enhancements in how
+foreign keys are handled with SQLite.  To check your version, 
+run this command:
 
     $ perl -MCatalyst::Model::DBIC::Schema -e \
         'print "$Catalyst::Model::DBIC::Schema::VERSION\n"'
-    0.31
+    0.4
 
 Please note the '\' above.  Depending on your environment, you might 
 be able to cut and paste the text as shown or need to remove the '\' 
 character to that the command is all on a single line.
 
-You should have version 0.31 or greater if you are following along 
-with Debian 5.  In other environments, you may need to run this 
-command to install it directly from CPAN:
+If you are following along in Debian 5, you should have version 0.40 or 
+higher (shown above as "0.4" with the tailing zero removed). If you have 
+less than v0.39, you will need to run this command to install it 
+directly from CPAN: 
 
     $ sudo cpan Catalyst::Model::DBIC::Schema
 
 And re-run the version print command to verify that you are now at 
-0.31 or higher.
+0.39 or higher.
+
+In addition, since we are using SQLite's foreign key support here,
+please be sure that you use version C<1.27> of L<DBD::SQLite> or later:
+
+    $ perl -MDBD::SQLite -e 'print "$DBD::SQLite::VERSION\n"'
+    1.29
+
+Upgrade if you are not at version C<1.27> or higher.
 
-Please use version C<1.27> of L<DBD::SQLite> or later for proper foreign key
-support.
 
 =head2 Create Static DBIx::Class Schema Files
 
@@ -760,9 +771,21 @@ into files.
 
 =item *
 
-And finally, C<dbi:SQLite:myapp.db> is the standard DBI connect string 
+C<dbi:SQLite:myapp.db> is the standard DBI connect string 
 for use with SQLite.
 
+=item *
+
+And finally, the C<on_connect_do> string requests that 
+L<DBIx::Class::Schema::Loader|DBIx::Class::Schema::Loader> create 
+foreign key relationships for us (this is not needed for databases such 
+as PostgreSQL and MySQL, but is required for SQLite). If you take a look 
+at C<lib/MyApp/Model/DB.pm>, you will see that the SQLite pragma is 
+propogated to the Model, so that SQLite's recent (and optional) foreign 
+key enforcement is enabled at the start of every database connection. 
+
+
+
 =back
 
 If you look in the C<lib/MyApp/Schema.pm> file, you will find that it 
@@ -812,6 +835,7 @@ L<DBIx::Class::Schema::Loader> version C<0.05000> or later.
         dbi:SQLite:myapp.db \
         on_connect_do="PRAGMA foreign_keys = ON"
 
+
 =head1 ENABLE THE MODEL IN THE CONTROLLER
 
 Open C<lib/MyApp/Controller/Books.pm> and un-comment the model code we 
@@ -825,20 +849,20 @@ and delete the next 2 lines):
     
     =cut
     
-    sub list : Local {
+    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];
+        # 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';
+        $c->stash(template => 'books/list.tt2');
     }
 
 B<TIP>: You may see the C<$c-E<gt>model('DB::Book')> un-commented 
@@ -868,9 +892,11 @@ and L<Catalyst::Model::DBIC::Schema|Catalyst::Model::DBIC::Schema>.
 First, let's enable an environment variable that causes 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:
+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
@@ -885,14 +911,13 @@ log).
 Then launch the Catalyst development server.  The log output should
 display something like:
 
-    $ script/myapp_server.pl
+    $ script/myapp_server.pl -r
     [debug] Debug messages enabled
     [debug] Statistics enabled
     [debug] Loaded plugins:
     .----------------------------------------------------------------------------.
     | Catalyst::Plugin::ConfigLoader  0.27                                       |
     | Catalyst::Plugin::StackTrace  0.11                                         |
-    | Catalyst::Plugin::Static::Simple  0.25                                     |
     '----------------------------------------------------------------------------'
     
     [debug] Loaded dispatcher "Catalyst::Dispatcher"
@@ -909,7 +934,7 @@ display something like:
     | MyApp::Model::DB::Author                                        | class    |
     | MyApp::Model::DB::Book                                          | class    |
     | MyApp::Model::DB::BookAuthor                                    | class    |
-    | MyApp::View::TT                                                 | instance |
+    | MyApp::View::HTML                                               | instance |
     '-----------------------------------------------------------------+----------'
     
     [debug] Loaded Private actions:
@@ -933,7 +958,7 @@ display something like:
     | /books/list                         | /books/list                          |
     '-------------------------------------+--------------------------------------'
     
-    [info] MyApp powered by Catalyst 5.80013
+    [info] MyApp powered by Catalyst 5.80020
     You can connect to your server at http://debian:3000
 
 B<NOTE:> Be sure you run the C<script/myapp_server.pl> command from
@@ -991,12 +1016,12 @@ will appear across your entire site/application instead of having to
 edit many individual files.
 
 
-=head2 Configure TT.pm For The Wrapper
+=head2 Configure HTML.pm For The Wrapper
 
 In order to create a wrapper, you must first edit your TT view and
 tell it where to find your wrapper file. 
 
-Edit you TT view in C<lib/MyApp/View/TT.pm> and change it to match the 
+Edit you TT view in C<lib/MyApp/View/HTML.pm> and change it to match the 
 following:
 
     __PACKAGE__->config(
@@ -1146,22 +1171,126 @@ provide lots of high-quality CSS functionality.
 
 =head2 Test Run The Application
 
-Restart the development server and hit "Reload" in your web browser
-and you should now see a formatted version of our basic book list.
-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.
+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 
+C<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
 
-Let's manually add some relationship information to the auto-generated 
-Result Class files. C<many_to_many> relationships are not currently
-automatically generated by L<DBIx::Class::Schema::Loader>.
-First edit C<lib/MyApp/Schema/Result/Book.pm> and add the 
-following text below the C<# You can replace this text...> comment:
+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 C<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" },
+    );
+
+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 mulitple 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 C<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
+
+    Related object: L<MyApp::Schema::Result::Book>
+
+    =cut
+
+    __PACKAGE__->belongs_to(
+      "book",
+      "MyApp::Schema::Result::Book",
+      { id => "book_id" },
+      { join_type => "LEFT" },
+    );
+
+The arguments are similar, but see 
+L<DBIx::Class::Relationship/belongs_to> for the details.
+
+Although recent versions of SQLite and L<DBIx::Class::Schema::Loader> 
+automatically handle the C<has_many> and C<belongs_to> relationships, 
+C<many_to_many> relationships currently need to be manually inserted. 
+To add a C<many_to_many> relationship, first edit 
+C<lib/MyApp/Schema/Result/Book.pm> and add the following text below 
+the C<# You can replace this text...> comment:
 
     # many_to_many():
     #   args:
@@ -1176,8 +1305,7 @@ file.  As with any Perl package, we need to end the last line with
 a statement that evaluates to C<true>.  This is customarily done with
 C<1;> on a line by itself.
 
-You'll notice there is already a C<has_many> relationship called
-C<book_authors>. The C<many_to_many> relationship is optional, but it makes it
+The C<many_to_many> relationship is optional, but it makes it
 easier to map a book to its collection of authors.  Without 
 it, we would have to "walk" though the C<book_author> table as in 
 C<$book-E<gt>book_author-E<gt>first-E<gt>author-E<gt>last_name> (we 
@@ -1189,9 +1317,10 @@ C<$book-E<gt>author-E<gt>first-E<gt>last_name>. Note that you cannot
 define a C<many_to_many> relationship without also having the 
 C<has_many> relationship in place.
 
-Then edit C<lib/MyApp/Schema/Result/Author.pm> and add relationship
-information as follows (again, be careful to put in above the C<1;> but
-below the C<# DO NOT MODIFY THIS OR ANYTHING ABOVE!> comment):
+Then edit C<lib/MyApp/Schema/Result/Author.pm> and add the reverse 
+C<many_to_many> relationship for C<Author> as follows (again, be careful 
+to put in above the C<1;> but below the C<# DO NOT MODIFY THIS OR 
+ANYTHING ABOVE!> comment): 
 
     # many_to_many():
     #   args:
@@ -1201,21 +1330,21 @@ below the C<# DO NOT MODIFY THIS OR ANYTHING ABOVE!> comment):
     #   You must already have the has_many() defined to use a many_to_many().
     __PACKAGE__->many_to_many(books => 'book_authors', 'book');
 
+
 =head2 Run The Application
 
 Run the Catalyst development server script with the C<DBIC_TRACE> option
 (it might still be enabled from earlier in the tutorial, but here is an
-alternate way to specify the option just in case):
+alternate way to specify the trace option just in case):
 
-    $ DBIC_TRACE=1 script/myapp_server.pl
+    $ DBIC_TRACE=1 script/myapp_server.pl -r
 
 Make sure that the application loads correctly and that you see the
 three dynamically created model class (one for each of the
 Result Classes we created).
 
 Then hit the URL L<http://localhost:3000/books/list> with your browser 
-and be sure that the book list still displays correctly. You can leave 
-the development server running for the next step if you wish.
+and be sure that the book list still displays correctly.
 
 B<Note:> You will not see the authors yet because the view does not yet 
 use the new relations. Read on to the next section where we update the 
@@ -1374,8 +1503,8 @@ detailed information on how to extend C<RenderView> in C<sub end>.
 One of the nice features of C<RenderView> is that it automatically 
 allows you to add C<dump_info=1> to the end of any URL for your 
 application and it will force the display of the "exception dump" 
-screen to the client browser.  You can try this out by starting the 
-development server as before and then point your browser to this URL: 
+screen to the client browser.  You can try this out by pointing 
+your browser to this URL: 
 
     http://localhost:3000/books/list?dump_info=1
 
@@ -1416,7 +1545,7 @@ has changed):
     
     =cut
     
-    sub list : Local {
+    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
@@ -1424,18 +1553,17 @@ has changed):
     
         # 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];
+        $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 (actions methods respond to user input in
         # your controllers).
-        #$c->stash->{template} = 'books/list.tt2';
+        #$c->stash(template => 'books/list.tt2');
     }
 
 
-You should now be able to restart the development server as per the
-previous section and access the L<http://localhost:3000/books/list>
-as before.
+You should now be able to access the L<http://localhost:3000/books/list>
+URL as before.
 
 B<NOTE:> Please note that if you use the default template technique,
 you will B<not> be able to use either the C<$c-E<gt>forward> or
@@ -1452,14 +1580,13 @@ In order to be able to use C<$c-E<gt>forward> and C<$c-E<gt>detach>
 later in the tutorial, you should remove the comment from the
 statement in C<sub list> in C<lib/MyApp/Controller/Books.pm>:
 
-    $c->stash->{template} = 'books/list.tt2';
+    $c->stash(template => 'books/list.tt2');
 
 Then delete the C<TEMPLATE_EXTENSION> line in
-C<lib/MyApp/View/TT.pm>.
+C<lib/MyApp/View/HTML.pm>.
 
-You should then be able to restart the development server and
-access L<http://localhost:3000/books/list> in the same manner as
-with earlier sections.
+Check the L<http://localhost:3000/books/list> URL in your browser.
+It should look the same manner as with earlier sections.
 
 
 =head1 AUTHOR
@@ -1470,5 +1597,5 @@ Please report any errors, issues or suggestions to the author.  The
 most recent version of the Catalyst Tutorial can be found at
 L<http://dev.catalyst.perl.org/repos/Catalyst/Catalyst-Manual/5.80/trunk/lib/Catalyst/Manual/Tutorial/>.
 
-Copyright 2006-2008, Kennedy Clark, under Creative Commons License
+Copyright 2006-2010, Kennedy Clark, under Creative Commons License
 (L<http://creativecommons.org/licenses/by-sa/3.0/us/>).