X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Manual.git;a=blobdiff_plain;f=lib%2FCatalyst%2FManual%2FTutorial%2F03_MoreCatalystBasics.pod;h=b8ef611217f04fb0b8e32bc05c83fb2939b1af0f;hp=7bae32b6e0fb5690d512c3136e4c2cc13d4ad553;hb=4768184b3b277399116fbd53cae3697a9767fee5;hpb=9887a877add998942bf080efb0b12aaf1df9a538 diff --git a/lib/Catalyst/Manual/Tutorial/03_MoreCatalystBasics.pod b/lib/Catalyst/Manual/Tutorial/03_MoreCatalystBasics.pod index 7bae32b..b8ef611 100644 --- a/lib/Catalyst/Manual/Tutorial/03_MoreCatalystBasics.pod +++ b/lib/Catalyst/Manual/Tutorial/03_MoreCatalystBasics.pod @@ -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 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 +object (generally written as C<$c>) that Catalyst passes to every +component throughout the framework. + By default, Catalyst enables three plugins/flags: @@ -200,20 +202,22 @@ For our application, we want to add one new plugin into the mix. To do this, edit C (this file is generally referred to as your I) and delete the lines with: - use Catalyst qw/-Debug - ConfigLoader - Static::Simple/; + use Catalyst qw/ + -Debug + ConfigLoader + Static::Simple + /; Then replace it with: # Load plugins use Catalyst qw/ - -Debug - ConfigLoader - Static::Simple - - StackTrace - /; + -Debug + ConfigLoader + Static::Simple + + StackTrace + /; B Recent versions of C have used a variety of techniques to load these plugins/flags. For example, you might see @@ -224,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, 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 @@ -233,7 +237,7 @@ browser, not in the console window from which you're running your application, which is where logging output usually goes. Make sure when adding new plugins you also include them as a new -dependancy within the Makefile.PL file. For example, after adding +dependency within the Makefile.PL file. For example, after adding the StackTrace plugin the Makefile.PL should include the following line: @@ -291,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 @@ -299,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: See Appendix 1 for tips on removing the leading spaces when @@ -321,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" in the code +attributes (the "C<:Local>" next to the "C" 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 @@ -432,22 +436,20 @@ but its use is now deprecated. Enter the following command to enable the C 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 (the second 'TT' argument) in -a file called C (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 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 in a file called C (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 and you should see that the default contents contains something similar to the following: __PACKAGE__->config(TEMPLATE_EXTENSION => '.tt'); @@ -541,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 and you should still get the Catalyst welcome page. Next, change the URL in your @@ -568,6 +570,7 @@ C 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 , @@ -575,8 +578,8 @@ C in your editor and enter: ); -- 'book_author' is a many-to-many join table between books & authors CREATE TABLE book_author ( - book_id INTEGER, - author_id INTEGER, + book_id INTEGER REFERENCES book(id) ON DELETE CASCADE ON UPDATE CASCADE, + author_id INTEGER REFERENCES author(id) ON DELETE CASCADE ON UPDATE CASCADE, PRIMARY KEY (book_id, author_id) ); CREATE TABLE author ( @@ -622,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 @@ -648,14 +652,14 @@ required if you do a single SQL statement on the command line). Use ".q" to exit from SQLite from the SQLite interactive mode and return to your OS command prompt. -Please note that here we have chosen to use 'singular' table names. This -is because the default inflection code for L -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 consistent -with it. If you prefer plural table names (e.g. they are easier and -more natural to read) then you will need to pass it an inflect_map -option. See L for more information. +Please note that here we have chosen to use 'singular' table names. This is +because the default inflection code for older versions of +L 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 +consistent with it. If you prefer plural table names (e.g. you think that they +are easier to read) then see the documentation in +L (version 0.05 or greater). For using other databases, such as PostgreSQL or MySQL, see L. @@ -686,25 +690,36 @@ 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, by -running this command: +L, 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 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. =head2 Create Static DBIx::Class Schema Files @@ -716,7 +731,8 @@ L and automatically build the required files for us: $ script/myapp_create.pl model DB DBIC::Schema MyApp::Schema \ - create=static dbi:SQLite:myapp.db + create=static dbi:SQLite:myapp.db \ + on_connect_do="PRAGMA foreign_keys = ON" 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 ... @@ -755,9 +771,21 @@ into files. =item * -And finally, C is the standard DBI connect string +C is the standard DBI connect string for use with SQLite. +=item * + +And finally, the C string requests that +L 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, 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 file, you will find that it @@ -797,31 +825,15 @@ 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): +use this process to automate the migration, but first make sure you have +version C<0.39> of L and +L version C<0.05000> or later. - $ # 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" + $ # Re-run the helper to upgrade for you $ script/myapp_create.pl model DB DBIC::Schema MyApp::Schema \ - create=static 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 - -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). + create=static naming=current use_namespaces=1 \ + dbi:SQLite:myapp.db \ + on_connect_do="PRAGMA foreign_keys = ON" =head1 ENABLE THE MODEL IN THE CONTROLLER @@ -837,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: You may see the C<$c-Emodel('DB::Book')> un-commented @@ -880,9 +892,11 @@ and L. 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 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 @@ -897,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" @@ -921,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: @@ -945,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 Be sure you run the C