X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Manual.git;a=blobdiff_plain;f=lib%2FCatalyst%2FManual%2FTutorial%2F04_BasicCRUD.pod;h=f84c3d743e284df29e30917c3d608450f46be53c;hp=4542b50f11ebf7d1a7122b4ce72c2e737424ed47;hb=b6e53c1ca5bfa271bfce99e0f42a56c8fd4df4be;hpb=3ab6187c1a123983b6ae29e57f543328ce15755c diff --git a/lib/Catalyst/Manual/Tutorial/04_BasicCRUD.pod b/lib/Catalyst/Manual/Tutorial/04_BasicCRUD.pod index 4542b50..f84c3d7 100644 --- a/lib/Catalyst/Manual/Tutorial/04_BasicCRUD.pod +++ b/lib/Catalyst/Manual/Tutorial/04_BasicCRUD.pod @@ -56,34 +56,36 @@ L =head1 DESCRIPTION -This chapter of the tutorial builds on the fairly primitive -application created in Chapter 3 to add basic support for Create, -Read, Update, and Delete (CRUD) of C objects. Note that the -'list' function in Chapter 2 already implements the Read portion of -CRUD (although Read normally refers to reading a single object; you -could implement full Read functionality using the techniques -introduced below). This section will focus on the Create and Delete -aspects of CRUD. More advanced capabilities, including full Update -functionality, will be addressed in Chapter 9. - -Although this chapter of the tutorial will show you how to build CRUD -functionality yourself, another option is to use a "CRUD builder" type -of tool to automate the process. You get less control, but it's quick -and easy. For example, see -L, -L, and -L. - -You can check out the source code for this example from the Catalyst -Subversion repository as per the instructions in -L. +This chapter of the tutorial builds on the fairly primitive application +created in +L to add +basic support for Create, Read, Update, and Delete (CRUD) of C +objects. Note that the 'list' function in +L already +implements the Read portion of CRUD (although Read normally refers to +reading a single object; you could implement full Read functionality +using the techniques introduced below). This section will focus on the +Create and Delete aspects of CRUD. More advanced capabilities, +including full Update functionality, will be addressed in +L. + +Although this chapter of the tutorial will show you how to build CRUD +functionality yourself, another option is to use a "CRUD builder" type +of tool to automate the process. You get less control, but it can be +quick and easy. For example, see L, +L, and L. + +Source code for the tutorial in included in the F +directory of the Tutorial Virtual machine (one subdirectory per +chapter). There are also instructions for downloading the code in +L. =head1 FORMLESS SUBMISSION -Our initial attempt at object creation will utilize the "URL -arguments" feature of Catalyst (we will employ the more common form- -based submission in the sections that follow). +Our initial attempt at object creation will utilize the "URL arguments" +feature of Catalyst (we will employ the more common form-based +submission in the sections that follow). =head2 Include a Create Action in the Books Controller @@ -91,45 +93,55 @@ based submission in the sections that follow). Edit C and enter the following method: =head2 url_create - + Create a book with the supplied title, rating, and author - + =cut - - sub url_create : Local { + + sub url_create :Local { # In addition to self & context, get the title, rating, & # author_id args from the URL. Note that Catalyst automatically # puts extra information after the "//model('DB::Book')->create({ title => $title, rating => $rating }); - + # Add a record to the join table for this book, mapping to # appropriate author - $book->add_to_book_author({author_id => $author_id}); + $book->add_to_book_authors({author_id => $author_id}); # Note: Above is a shortcut for this: - # $book->create_related('book_author', {author_id => $author_id}); - - # Assign the Book object to the stash for display in the view - $c->stash->{book} = $book; - - # Set the TT template to use - $c->stash->{template} = 'books/create_done.tt2'; + # $book->create_related('book_authors', {author_id => $author_id}); + + # Assign the Book object to the stash for display and set template + $c->stash(book => $book, + template => 'books/create_done.tt2'); + + # Disable caching for this page + $c->response->header('Cache-Control' => 'no-cache'); } Notice that Catalyst takes "extra slash-separated information" from the -URL and passes it as arguments in C<@_>. The C action then -uses a simple call to the DBIC C method to add the requested -information to the database (with a separate call to -C to update the join table). As do virtually all -controller methods (at least the ones that directly handle user input), -it then sets the template that should handle this request. +URL and passes it as arguments in C<@_> (as long as the number of +arguments is not "fixed" using an attribute like C<:Args(0)>). The +C action then uses a simple call to the DBIC C +method to add the requested information to the database (with a separate +call to C to update the join table). As do +virtually all controller methods (at least the ones that directly handle +user input), it then sets the template that should handle this request. + +Also note that we are explicitly setting a C "Cache-Control" +header to force browsers using the page to get a fresh copy every time. +You could even move this to a C method in +C and it would automatically get applied +to every page in the whole application via a single line of code +(remember from Chapter 3, that every C method gets run in the +Controller hierarchy). =head2 Include a Template for the 'url_create' Action: @@ -140,64 +152,51 @@ Edit C and then enter: [% # Not a good idea for production use, though. :-) 'Indent=1' is -%] [% # optional, but prevents "massive indenting" of deeply nested objects -%] [% USE Dumper(Indent=1) -%] - + [% # Set the page title. META can 'go back' and set values in templates -%] - [% # that have been processed 'before' this template (here it's for -%] - [% # root/lib/site/html and root/lib/site/header). Note that META only -%] - [% # works on simple/static strings (i.e. there is no variable -%] - [% # interpolation). -%] + [% # that have been processed 'before' this template (here it's updating -%] + [% # the title in the root/src/wrapper.tt2 wrapper template). Note that -%] + [% # META only works on simple/static strings (i.e. there is no variable -%] + [% # interpolation -- if you need dynamic/interpolated content in your -%] + [% # title, set "$c->stash(title => $something)" in the controller). -%] [% META title = 'Book Created' %] - - [% # Output information about the record that was added. First title. -%] + + [% # Output information about the record that was added. First title. -%]

Added book '[% book.title %]' - - [% # Output the last name of the first author. This is complicated by an -%] - [% # issue in TT 2.15 where blessed hash objects are not handled right. -%] - [% # First, fetch 'book.author' from the DB once. -%] - [% authors = book.author %] - [% # Now use IF statements to test if 'authors.first' is "working". If so, -%] - [% # we use it. Otherwise we use a hack that seems to keep TT 2.15 happy. -%] - by '[% authors.first.last_name IF authors.first; - authors.list.first.value.last_name IF ! authors.first %]' - - [% # Output the rating for the book that was added -%] + + [% # Then, output the last name of the first author -%] + by '[% book.authors.first.last_name %]' + + [% # Then, output the rating for the book that was added -%] with a rating of [% book.rating %].

- - [% # Provide a link back to the list page -%] - [% # 'uri_for()' builds a full URI; e.g., 'http://localhost:3000/books/list' -%] + + [% # Provide a link back to the list page. 'c.uri_for' builds -%] + [% # a full URI; e.g., 'http://localhost:3000/books/list' -%]

Return to list

- + [% # Try out the TT Dumper (for development only!) -%]
     Dump of the 'book' variable:
     [% Dumper.dump(book) %]
     
-The TT C directive allows access to a variety of plugin modules -(TT plugins, that is, not Catalyst plugins) to add extra functionality -to the base TT capabilities. Here, the plugin allows -L "pretty printing" of objects and -variables. Other than that, the rest of the code should be familiar -from the examples in Chapter 3. +The TT C directive allows access to a variety of plugin modules (TT +plugins, that is, not Catalyst plugins) to add extra functionality to +the base TT capabilities. Here, the plugin allows L +"pretty printing" of objects and variables. Other than that, the rest +of the code should be familiar from the examples in Chapter 3. =head2 Try the 'url_create' Feature -If the application is still running from before, use C to kill -it. Then restart the server: +Make sure the development server is running with the "-r" restart +option: - $ DBIC_TRACE=1 script/myapp_server.pl + $ DBIC_TRACE=1 script/myapp_server.pl -r Note that new path for C appears in the startup debug output. -B: You can use C