X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Manual.git;a=blobdiff_plain;f=lib%2FCatalyst%2FManual%2FTutorial%2FMoreCatalystBasics.pod;h=e125cd748a9c8af0bc44b28088ecb19b64772b2b;hp=2b94d3aa0b1efbc4c23b8be06c1d6fb2797ee43e;hb=4ab6212da7a5e07df9837b0e57fb2b0c37aa9759;hpb=acbd7bddabb7f0d62f0e031e21d440a0ae496d6d diff --git a/lib/Catalyst/Manual/Tutorial/MoreCatalystBasics.pod b/lib/Catalyst/Manual/Tutorial/MoreCatalystBasics.pod index 2b94d3a..e125cd7 100644 --- a/lib/Catalyst/Manual/Tutorial/MoreCatalystBasics.pod +++ b/lib/Catalyst/Manual/Tutorial/MoreCatalystBasics.pod @@ -1,11 +1,11 @@ =head1 NAME -Catalyst::Manual::Tutorial::MoreCatalystBasics - Catalyst Tutorial - Part 3: More Catalyst Application Development Basics +Catalyst::Manual::Tutorial::MoreCatalystBasics - Catalyst Tutorial - Chapter 3: More Catalyst Application Development Basics =head1 OVERVIEW -This is B for the Catalyst tutorial. +This is B for the Catalyst tutorial. L @@ -56,13 +56,13 @@ L =head1 DESCRIPTION -This part of the tutorial builds on the work done in Part 2 to explore -some features that are more typical of "real world" web applications. -From this part of the tutorial onward, we will be building a simple -book database application. Although the application will be too -limited to be of use to anyone, it should provide a basic environment -where we can explore a variety of features used in virtually all web -applications. +This chapter of the tutorial builds on the work done in Chapter 2 to +explore some features that are more typical of "real world" web +applications. From this chapter of the tutorial onward, we will be +building a simple book database application. Although the application +will be too limited to be of use to anyone, it should provide a basic +environment where we can explore a variety of features used in +virtually all web applications. You can checkout the source code for this example from the catalyst subversion repository as per the instructions in @@ -74,7 +74,7 @@ L. The remainder of the tutorial will build an application called C. First use the Catalyst C script to initialize the framework for the C application (make sure you aren't still inside the -directory of the C application from the previous part of the +directory of the C application from the previous chapter of the tutorial or in a directory that already has a "MyApp" subdirectory): $ catalyst.pl MyApp @@ -86,7 +86,7 @@ tutorial or in a directory that already has a "MyApp" subdirectory): created "MyApp/script/myapp_create.pl" $ cd MyApp -This creates a similar skeletal structure to what we saw in Part 2 of +This creates a similar skeletal structure to what we saw in Chapter 2 of the tutorial, except with C and C substituted for C and C. @@ -138,7 +138,7 @@ L file (versus having the values hard-coded inside your Perl modules). Config::General uses syntax very similar to Apache configuration files. We will see how to use this feature of Catalyst during the authentication and authorization -sections (Part 5 and Part 6). +sections (Chapter 5 and Chapter 6). B If you are using a version of L prior to version 1.06, be aware @@ -247,7 +247,7 @@ actions: created "/home/me/MyApp/script/../lib/MyApp/Controller/Books.pm" created "/home/me/MyApp/script/../t/controller_Books.t" -Then edit C (as discussed in Part 2 of +Then edit C (as discussed in Chapter 2 of the Tutorial, Catalyst has a separate directory under C for each of the three parts of MVC: C, C, and C) and add the following method to the controller: @@ -374,7 +374,7 @@ types not discussed here (C and C). =head1 CATALYST VIEWS -As mentioned in Part 2 of the tutorial, views are where you render +As mentioned in Chapter 2 of the tutorial, views are where you render output, typically for display in the user's web browser (but also possibly using other display output-generation systems). The code in C selects the I of view to use, with the actual @@ -407,7 +407,7 @@ L Both helpers are similar. C creates the C file and leaves the creation of any hierarchical template organization entirely up to you. (It also creates a C file for testing; -test cases will be discussed in Part 8.) C, on the other hand, +test cases will be discussed in Chapter 8.) C, on the other hand, creates a modular and hierarchical view layout with separate Template Toolkit (TT) files for common header and footer information, configuration values, a CSS stylesheet, and more. @@ -655,7 +655,8 @@ Use the C model helper option to build a model that dynamically reads your database structure every time the application starts: - $ script/myapp_create.pl model DB DBIC::Schema MyApp::Schema create=dynamic dbi:SQLite:myapp.db + $ script/myapp_create.pl model DB DBIC::Schema MyApp::Schema \ + create=dynamic dbi:SQLite:myapp.db exists "/home/me/MyApp/script/../lib/MyApp/Model" exists "/home/me/MyApp/script/../t" exists "/home/me/MyApp/script/../lib/MyApp" @@ -970,7 +971,7 @@ B The Catalyst stash only lasts for a single HTTP request. If you need to retain information across requests you can use L (we will use -Catalyst sessions in the Authentication part of the tutorial). +Catalyst sessions in the Authentication chapter of the tutorial). =item * @@ -1047,6 +1048,23 @@ template -- the wrapper will provide the overall feel of the page. =head1 A STATIC DATABASE MODEL WITH C +First, let's be sure we have a recent versino of the DBIC helper, +L, by +running this command: + + $ perl -MCatalyst::Model::DBIC::Schema -e \ + 'print "$Catalyst::Model::DBIC::Schema::VERSION\n"' + 0.23 + +If you don't have version 0.23 or higher, please 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.23 or higher. + + =head2 Create Static DBIC Schema Files Unlike the previous DBIC section where we had C @@ -1064,7 +1082,8 @@ First, lets remove the schema file created earlier: Now regenerate the schema using the C option: - $ script/myapp_create.pl model DB DBIC::Schema MyApp::Schema create=static dbi:SQLite:myapp.db + $ script/myapp_create.pl model DB DBIC::Schema MyApp::Schema \ + create=static components=TimeStamp dbi:SQLite:myapp.db exists "/home/me/MyApp/script/../lib/MyApp/Model" exists "/home/me/MyApp/script/../t" Dumping manual schema for MyApp::Schema to directory /home/me/MyApp/script/../lib ... @@ -1083,8 +1102,9 @@ L as its base class (L is only being used by the helper to load the schema once and then create the static files for us) and C only contains a call to the -C method. You will also find that C contains -a C subdirectory, with files inside this directory named +C method. You will also find that C +contains a C subdirectory, which then has a subdirectory +called "Result". This "Result" subdirectory then has files named according to each of the tables in our simple database (C, C, and C). These three files are called "Result Classes" in DBIC nomenclature. Although the Result Class files @@ -1094,39 +1114,64 @@ especially in L. The idea with the Result Source files created under -C by the C option is to only edit the -files below the C<# DO NOT MODIFY THIS OR ANYTHING ABOVE!> warning. -If you place all of your changes below that point in the file, you can -regenerate the automatically created information at the top of each -file should your database structure get updated. +C by the C option is to only +edit the files below the C<# DO NOT MODIFY THIS OR ANYTHING ABOVE!> +warning. If you place all of your changes below that point in the +file, you can regenerate the automatically created information at the +top of each file should your database structure get updated. Also note the "flow" of the model information across the various files and directories. Catalyst will initially load the model from C. This file contains a reference to C, so that file is loaded next. Finally, the -call to C in C will load each of the "result -class" files from the C subdirectory. The end -result is that Catalyst will dynamically create three table-specific -Catalyst models every time the application starts (you can see these -three model files listed in the debug output generated when you launch -the application). +call to C in C will load each of the +"Result Class" files from the C subdirectory. +The final outcome is that Catalyst will dynamically create three +table-specific Catalyst models every time the application starts (you +can see these three model files listed in the debug output generated +when you launch the application). + +B Older versions of +L use the +deprecated DBIC C technique instead of the newer +C. For new applications, please try to use +C since it more easily supports a very useful DBIC +technique called "ResultSet Classes." If you need to convert an +existing application from "load_classes" to "load_namespaces," you can +use this process to automate the migration (but first make sure you +have v0.23 C as discussed above): + + $ # First delete the existing schema file to disable "compatibility" mode + $ rm lib/MyApp/Schema.pm + $ + $ # Then re-run the helper to build the files for "load_namespaces" + $ script/myapp_create.pl model DB DBIC::Schema MyApp::Schema \ + create=static components=TimeStamp dbi:SQLite:myapp.db + $ + $ # Now convert the existing files over + $ cd lib/MyApp/Schema + $ perl -MIO::All -e 'for (@ARGV) { my $s < io($_); $s =~ s/.*\n\# You can replace.*?\n//s; + $s =~ s/'MyApp::Schema::/'MyApp::Schema::Result::/g; my $d < io("Result/$_"); + $d =~ s/1;\n?//; "$d$s" > io("Result/$_"); }' *.pm + $ cd ../../.. + $ + $ # And finally delete the old files + $ rm lib/MyApp/Schema/*.pm -B The version of -L in -Debian 5 uses the older DBIC C vs. the newer -C technique. For new applications, please try to use -C since it more easily supports a very useful DBIC -technique called "ResultSet Classes." We will migrate to -C in Part 4 (BasicCRUD) of this tutorial. +The "C" script will copy all the customized +relationship (and other) information below "C<# DO NOT MODIFY>" line +from the old files in C to the new files in +C (we will be starting to add some +"customized relationship information in the section below). -=head2 Updating the Generated DBIC Schema Files +=head2 Updating the Generated DBIC Result Class Files Let's manually add some relationship information to the auto-generated Result Class files. (Note: if you are using a database other than SQLite, such as PostgreSQL, then the relationship could have been automatically placed in the Result Class files. If so, you can skip -this step.) First edit C and add the +this step.) First edit C and add the following text below the C<# You can replace this text...> comment: # @@ -1138,7 +1183,7 @@ following text below the C<# You can replace this text...> comment: # 1) Name of relationship, DBIC will create accessor with this name # 2) Name of the model class referenced by this relationship # 3) Column name in *foreign* table (aka, foreign key in peer table) - __PACKAGE__->has_many(book_authors => 'MyApp::Schema::BookAuthors', 'book_id'); + __PACKAGE__->has_many(book_authors => 'MyApp::Schema::Result::BookAuthors', 'book_id'); # many_to_many(): # args: @@ -1178,7 +1223,7 @@ C<$book-Eauthors-Efirst-Elast_name>. Note that you cannot define a C relationship without also having the C relationship in place. -Then edit C and add relationship +Then edit C 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): @@ -1191,7 +1236,7 @@ below the C<# DO NOT MODIFY THIS OR ANYTHING ABOVE!> comment): # 1) Name of relationship, DBIC will create accessor with this name # 2) Name of the model class referenced by this relationship # 3) Column name in *foreign* table (aka, foreign key in peer table) - __PACKAGE__->has_many(book_author => 'MyApp::Schema::BookAuthors', 'author_id'); + __PACKAGE__->has_many(book_author => 'MyApp::Schema::Result::BookAuthors', 'author_id'); # many_to_many(): # args: @@ -1202,7 +1247,7 @@ below the C<# DO NOT MODIFY THIS OR ANYTHING ABOVE!> comment): __PACKAGE__->many_to_many(books => 'book_author', 'book'); Finally, do the same for the "join table," -C: +C: # # Set relationships: @@ -1213,14 +1258,14 @@ C: # 1) Name of relationship, DBIC will create accessor with this name # 2) Name of the model class referenced by this relationship # 3) Column name in *this* table - __PACKAGE__->belongs_to(book => 'MyApp::Schema::Books', 'book_id'); + __PACKAGE__->belongs_to(book => 'MyApp::Schema::Result::Books', 'book_id'); # belongs_to(): # args: # 1) Name of relationship, DBIC will create accessor with this name # 2) Name of the model class referenced by this relationship # 3) Column name in *this* table - __PACKAGE__->belongs_to(author => 'MyApp::Schema::Authors', 'author_id'); + __PACKAGE__->belongs_to(author => 'MyApp::Schema::Result::Authors', 'author_id'); =head2 Run The Application @@ -1233,7 +1278,7 @@ is an alternate way to specify the option just in case): Make sure that the application loads correctly and that you see the three dynamically created model class (one for each of the -table-specific schema classes we created). +Result Classes we created). Then hit the URL L with your browser and be sure that the book list is displayed via the relationships @@ -1330,8 +1375,8 @@ and asked your browser to view the page source. =head1 OPTIONAL INFORMATION -B, +B, if you wish.> @@ -1426,8 +1471,8 @@ as before. B Please note that if you use the default template technique, you will B be able to use either the C<$c-Eforward> or -the C<$c-Edetach> mechanisms (these are discussed in Part 2 and -Part 9 of the Tutorial). +the C<$c-Edetach> mechanisms (these are discussed in Chapter 2 and +Chapter 9 of the Tutorial). =head2 Return To A Manually-Specified Template