X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Manual.git;a=blobdiff_plain;f=lib%2FCatalyst%2FManual%2FTutorial%2FBasicCRUD.pod;h=463e8d5acd458bdc4f8bc00d859c420fdc731d8f;hp=1ae6394e5072386bb8c4ea34ad446f1e6c067c18;hb=3b1fa91be1d89d2297aa9e8e83462344d9cd9820;hpb=554908172d7b73d43084ac283bb2dad6c7283e92 diff --git a/lib/Catalyst/Manual/Tutorial/BasicCRUD.pod b/lib/Catalyst/Manual/Tutorial/BasicCRUD.pod index 1ae6394..463e8d5 100644 --- a/lib/Catalyst/Manual/Tutorial/BasicCRUD.pod +++ b/lib/Catalyst/Manual/Tutorial/BasicCRUD.pod @@ -1,11 +1,11 @@ =head1 NAME -Catalyst::Manual::Tutorial::BasicCRUD - Catalyst Tutorial - Part 4: Basic CRUD +Catalyst::Manual::Tutorial::BasicCRUD - Catalyst Tutorial - Chapter 4: Basic CRUD =head1 OVERVIEW -This is B for the Catalyst tutorial. +This is B for the Catalyst tutorial. L @@ -56,26 +56,26 @@ L =head1 DESCRIPTION -This part of the tutorial builds on the fairly primitive application -created in Part 3 to add basic support for Create, Read, Update, and -Delete (CRUD) of C objects. Note that the 'list' function in Part -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 -Part 9. - -Although this part 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 +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 checkout the source code for this example from the catalyst -subversion repository as per the instructions in +You can check out the source code for this example from the Catalyst +Subversion repository as per the instructions in L. @@ -105,28 +105,20 @@ Edit C and enter the following method: # Call create() on the book model object. Pass the table # columns/field values we want to set as hash values - my $book = $c->model('DB::Books')->create({ + my $book = $c->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_authors({author_id => $author_id}); + $book->add_to_book_author({author_id => $author_id}); # Note: Above is a shortcut for this: - # $book->create_related('book_authors', {author_id => $author_id}); + # $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; - # This is a hack to disable XSUB processing in Data::Dumper - # (it's used in the view). This is a work-around for a bug in - # the interaction of some versions or Perl, Data::Dumper & DBIC. - # You won't need this if you aren't using Data::Dumper (or if - # you are running DBIC 0.06001 or greater), but adding it doesn't - # hurt anything either. - $Data::Dumper::Useperl = 1; - # Set the TT template to use $c->stash->{template} = 'books/create_done.tt2'; } @@ -135,12 +127,12 @@ 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 +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. -=head2 Include a Template for the C Action: +=head2 Include a Template for the 'url_create' Action: Edit C and then enter: @@ -151,8 +143,9 @@ Edit C and then enter: [% # 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 on -%] - [% # simple strings (e.g., no variable interpolation). -%] + [% # 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). -%] [% META title = 'Book Created' %] [% # Output information about the record that was added. First title. -%] @@ -160,8 +153,8 @@ Edit C and then enter: [% # 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.authors' from the DB once. -%] - [% authors = book.authors %] + [% # 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; @@ -185,10 +178,10 @@ The TT C directive allows access to a variety of plugin modules 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 Part 3. +from the examples in Chapter 3. -=head2 Try the C Feature +=head2 Try the 'url_create' Feature If the application is still running from before, use C to kill it. Then restart the server: @@ -215,11 +208,8 @@ object as it was returned by DBIC. You should also see the following DBIC debug messages displayed in the development server log messages if you have DBIC_TRACE set: - INSERT INTO books (rating, title) VALUES (?, ?): `5', `TCPIP_Illustrated_Vol-2' - INSERT INTO book_authors (author_id, book_id) VALUES (?, ?): `4', `6' - SELECT author.id, author.first_name, author.last_name - FROM book_authors me JOIN authors author - ON ( author.id = me.author_id ) WHERE ( me.book_id = ? ): '6' + INSERT INTO book (rating, title) VALUES (?, ?): `5', `TCPIP_Illustrated_Vol-2' + INSERT INTO book_author (author_id, book_id) VALUES (?, ?): `4', `6' The C statements are obviously adding the book and linking it to the existing record for Richard Stevens. The C