From: Kennedy Clark Date: Thu, 5 Mar 2009 17:57:54 +0000 (+0000) Subject: Add a new section to BasicCRUD covering more advanced features of DBIC ("EXPLORING... X-Git-Tag: v5.8005~192 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Manual.git;a=commitdiff_plain;h=1cde0fd603f0f21f0197632bd19904f468f28c83 Add a new section to BasicCRUD covering more advanced features of DBIC ("EXPLORING THE POWER OF DBIC") plus a few other minor changes --- diff --git a/Changes b/Changes index b00cf9d..5153988 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,9 @@ Revision history for Catalyst-Manual +5.7XXX XXXXXX + - Add a new section to BasicCRUD covering more advanced features of + DBIC ("EXPLORING THE POWER OF DBIC") + 5.7018 2 Mar 2009 - Suggestions and fixes with thanks to mintywalker@gmail.com - DBIC-related updates in MoreCatalystBasics diff --git a/lib/Catalyst/Manual/Tutorial/Appendices.pod b/lib/Catalyst/Manual/Tutorial/Appendices.pod index 2195e63..c8037df 100644 --- a/lib/Catalyst/Manual/Tutorial/Appendices.pod +++ b/lib/Catalyst/Manual/Tutorial/Appendices.pod @@ -321,7 +321,8 @@ Delete the existing model: Regenerate the model using the Catalyst "_create.pl" script: - script/myapp_create.pl model MyAppDB DBIC::Schema MyApp::Schema dbi:mysql:myapp 'tutorial' '' '{ AutoCommit => 1 }' + script/myapp_create.pl model MyAppDB DBIC::Schema MyApp::Schema \ + dbi:mysql:myapp '_username_here_' '_password_here_' '{ AutoCommit => 1 }' =back diff --git a/lib/Catalyst/Manual/Tutorial/BasicCRUD.pod b/lib/Catalyst/Manual/Tutorial/BasicCRUD.pod index 6a8278f..598f3e2 100644 --- a/lib/Catalyst/Manual/Tutorial/BasicCRUD.pod +++ b/lib/Catalyst/Manual/Tutorial/BasicCRUD.pod @@ -407,14 +407,14 @@ method: sub base :Chained('/') :PathPart('books') :CaptureArgs(0) { my ($self, $c) = @_; - # Store the resultset in stash so it's available for other methods + # Store the ResultSet in stash so it's available for other methods $c->stash->{resultset} = $c->model('DB::Books'); # Print a message to the debug log $c->log->debug('*** INSIDE BASE METHOD ***'); } -Here we print a log message and store the DBIC resultset in +Here we print a log message and store the DBIC ResultSet in C<$c-Estash-E{resultset}> so that it's automatically available for other actions that chain off C. If your controller always needs a book ID as it's first argument, you could have the base method @@ -585,7 +585,7 @@ from the database. =head2 Include a Delete Link in the List -Edit C and update it to the following (two +Edit C and update it to match the following (two sections have changed: 1) the additional 'Links' table header, and 2) the four lines for the Delete link near the bottom). @@ -928,6 +928,461 @@ that should really go to Window A. For this reason, you may wish to use the "query param" technique shown here in your applications. +=head1 EXPLORING THE POWER OF DBIC + +In this section we will explore some additional capabilities offered +by DBIx::Class. Although these features have relatively little to do +with Catalyst per-se, you will almost certainly want to take advantage +of them in your applications. + + +=head2 Convert to DBIC "load_namespaces" + +If you look back at +L you will recall that we load our DBIC Result Classes +(Books.pm, Authors.pm and BookAuthors.pm) with in +C with the C feature. Although +this method is perfectly valid, the DBIC community has migrated to a +newer C technique because it more easily supports a +variety of advanced features. Since we want to explore some of these +features below, let's first migrate our configuration over to use +C. + +If you are following along in Ubuntu 8.10, you will need to +upgrade your version of +L +to 0.22 or higher. To do this, we can install directly from CPAN: + + $ cpan Catalyst::Model::DBIC::Schema + +Then make sure you are running an appropriate version: + + $ perl -MCatalyst::Model::DBIC::Schema -e \ + 'print "$Catalyst::Model::DBIC::Schema::VERSION\n"' + 0.22 + +Make sure you get version 0.22 or higher. + +B Utuntu will automatically "do the right thing" and use the +module we installed from CPAN and ignore the older version we picked +up via the C command. If you are using a different +environment, you will need to make sure you are using v0.22 or higher +with the command above. + +While we are at it, let's install a few other modules from CPAN for +some of the other work we will be doing below: + + $ cpan Time::Warp DBICx::TestDatabase \ + DBIx::Class::DynamicDefault DBIx::Class::TimeStamp + +Next, we need to delete the existing C so that +the Catalyst DBIC helper will recreate it. Then we re-generate +the model and schema information: + + $ rm lib/MyApp/Schema.pm + $ script/myapp_create.pl model DB DBIC::Schema MyApp::Schema \ + create=static components=TimeStamp dbi:SQLite:myapp.db + exists "/root/dev/MyApp/script/../lib/MyApp/Model" + exists "/root/dev/MyApp/script/../t" + Dumping manual schema for MyApp::Schema to directory /root/dev/MyApp/script/../lib ... + Schema dump completed. + exists "/root/dev/MyApp/script/../lib/MyApp/Model/DB.pm" + $ + $ ls lib/MyApp/Schema + Authors.pm BookAuthors.pm Books.pm Result + $ ls lib/MyApp/Schema/Result + Authors.pm BookAuthors.pm Books.pm + +Notice that we now have a duplicate set of Result Class files. With +the newer C feature, DBIC automatically looks for +your Result Class files in a subdirectory of the Schema directory +called C (the files in C were already there +from Part 3 of the tutorial; the files in C +are new). + +If you are using SQLite, you will need to manually re-enter the +relationship configuration as we did in Part 3 of the tutorial (if you +are using different database, the relationships might have been auto- +generated by Schema::Loader). One option is to use the following +command-line perl script to migrate the information across +automatically: + + $ 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 ../../.. + +If you prefer, you can do the migration by hand using "cut and paste" +from the files in C (or from +L) +to the corresponding files in C. If you take +this approach, be sure to add C<::Result> to the end of +C in all three files (for example, in C, the +"peer class" in the C relationship needs to be changed from +C to C). + +Now we can remove the original set of Result Class files that we no +longer need: + + $ rm lib/MyApp/Schema/*.pm + $ ls lib/MyApp/Schema + Result + +Finally, test the application to be sure everything is still +working under our new configuration. Use the +C