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<CatalystX::ListFramework::Builder|CatalystX::ListFramework::Builder>,
+L<Catalyst::Plugin::AutoCRUD|Catalyst::Plugin::AutoCRUD>,
L<CatalystX::CRUD|CatalystX::CRUD>, and
L<CatalystX::CRUD::YUI|CatalystX::CRUD::YUI>.
to get around an issue in TT v2.15 where blessed hash objects were not
handled correctly. But, if you are still using v2.15, it's probably
-time to upgrade (v2.15 is exactly 3 years old on the day I'm typing
-this). If you are following along in Debian, then you should be on at
-least v2.20. You can test your version of Template Toolkit with the
-following:
+time to upgrade (v2.15 is 3.5+ years old). If you are following
+along in Debian, then you should be on at least v2.20. You can test
+your version of Template Toolkit with the following:
perl -MTemplate -e 'print "$Template::VERSION\n"'
see the six DBIC debug messages similar to the following (where
N=1-6):
- SELECT author.id, author.first_name, author.last_name \
- FROM book_author me JOIN author author \
+ SELECT author.id, author.first_name, author.last_name
+ FROM book_author me JOIN author author
ON author.id = me.author_id WHERE ( me.book_id = ? ): 'N'
-(The '\' characters won't actually appear in the output -- we are
-using them as "line continuation markers" here.)
-
=head1 CONVERT TO A CHAINED ACTION
the C<url_create> method because this method "chained" off C<base> and
specified C<:PathPart('url_create')> (note that we could have omitted
the "PathPart" here because it matches the name of the method, but we
-will include it to make the logic behind the tutorial as explicit as
-possible).
+will include it to make the logic as explicit as possible).
Once again, enter the following URL into your browser:
Open C<root/src/books/form_create.tt2> in your editor and enter:
[% META title = 'Manual Form Book Create' -%]
-
+
<form method="post" action="[% c.uri_for('form_create_do') %]">
<table>
<tr><td>Title:</td><td><input type="text" name="title"></td></tr>
with the new fields:
$ script/myapp_create.pl model DB DBIC::Schema MyApp::Schema \
- create=static components=TimeStamp dbi:SQLite:myapp.db
+ create=static components=TimeStamp dbi:SQLite:myapp.db \
+ on_connect_do="PRAGMA foreign_keys = ON"
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 ...
you will see that the new book we added has an appropriate date and
time entered for it (see the last line in the listing below):
- sqlite3 myapp.db "select * from book"
+ $ sqlite3 myapp.db "select * from book"
1|CCSP SNRS Exam Certification Guide|5|2009-03-08 16:26:35|2009-03-08 16:26:35
2|TCP/IP Illustrated, Volume 1|5|2009-03-08 16:26:35|2009-03-08 16:26:35
3|Internetworking with TCP/IP Vol.1|4|2009-03-08 16:26:35|2009-03-08 16:26:35
method that returns books added in the last 10 minutes. Start by
making a directory where DBIx::Class will look for our ResultSet Class:
- mkdir lib/MyApp/Schema/ResultSet
+ $ mkdir lib/MyApp/Schema/ResultSet
Then open C<lib/MyApp/Schema/ResultSet/Book.pm> and enter the following:
sub created_after {
my ($self, $datetime) = @_;
- my $date_str = $self->_source_handle->schema->storage
+ my $date_str = $self->result_source->schema->storage
->datetime_parser->format_datetime($datetime);
return $self->search({
1;
-Then we need to tell the Result Class to to treat this as a ResultSet
-Class. Open C<lib/MyApp/Schema/Result/Book.pm> and add the following
-above the "C<1;>" at the bottom of the file:
-
- #
- # Set ResultSet Class
- #
- __PACKAGE__->resultset_class('MyApp::Schema::ResultSet::Book');
-
Then add the following method to the C<lib/MyApp/Controller/Books.pm>:
=head2 list_recent
The "Author(s)" column will now contain both the first and last name.
And, because the concatenation logic was encapsulated inside our
-Result Class, it keeps the code inside our .tt template nice and clean
+Result Class, it keeps the code inside our TT template nice and clean
(remember, we want the templates to be as close to pure HTML markup as
possible). Obviously, this capability becomes even more useful as you
use to to remove even more complicated row-specific logic from your
return the number of authors for a book. Open
C<lib/MyApp/Schema/Result/Book.pm> and add the following method:
-=head2 author_count
-
-Return the number of authors for the current book
-
+ =head2 author_count
+
+ Return the number of authors for the current book
+
=cut
sub author_count {
make your code cleaner, easier to write, less error-prone, and easier
to debug and maintain.
+Before you conclude this section, fire up the development server and
+hit Refresh in your browser... the output should be the same even
+though the backend code has been trimmed down.
+
=head1 AUTHOR