=item * B<Model>
-Access and modify content (data). L<Class::DBI>, L<Plucene>, L<Net::LDAP>...
+Access and modify content (data). L<DBIx::Class>, L<Class::DBI>,
+L<Plucene>, L<Net::LDAP>...
=item * B<View>
=item * B<Pre-Built Components for Popular Modules>
-See L<Catalyst::Model::CDBI> for L<Class::DBI>, or L<Catalyst::View::TT>
-for L<Template Toolkit|Template>.
+See L<Catalyst::Model::DBIC::Schema> for L<DBIx::Class>, or
+L<Catalyst::View::TT> for L<Template Toolkit|Template>.
=item * B<Built-in Test Framework>
my $first_argument = $c->req->args[0]; # now = 'test1'
# do something...
}
-
+
As you can see from these examples, you can just use the method name as
long as you are referring to methods in the same controller. If you want
to forward to a method in another controller, or the main application,
=head4 Models
To show how to define models, again we'll use an already-existing base
-class, this time for L<Class::DBI>: L<Catalyst::Model::CDBI>.
+class, this time for L<DBIx::Class>: L<Catalyst::Model::DBIC::Schema>.
+We'll also need L<DBIx::Class::Schema::Loader>.
But first, we need a database.
% sqlite /tmp/myapp.db < myapp.sql
-Now we can create a CDBI component for this database.
-
- package MyApp::Model::CDBI;
-
- use strict;
- use base 'Catalyst::Model::CDBI';
-
- __PACKAGE__->config(
- dsn => 'dbi:SQLite:/tmp/myapp.db',
- relationships => 1
- );
+Now we can create a DBIC::SchemaLoader component for this database.
- 1;
+ script/myapp_create.pl model DBIC DBIC::SchemaLoader 'dbi:SQLite:/tmp/myapp.db'
-Catalyst automatically loads table layouts and relationships. Use the
-stash to pass data to your templates.
+L<DBIx::Class::Schema::Loader> automatically loads table layouts and
+relationships. Use the stash to pass data to your templates.
- package MyApp;
+We add the following to MyApp/Controller/Root.pm
- use strict;
- use Catalyst '-Debug';
+ sub view : Global {
+ my ( $self, $c, $id ) = @_;
+
+ $c->stash->{item} = $c->model('DBIC::Foo')->find($id);
+ }
- __PACKAGE__->config(
- name => 'My Application',
- root => '/home/joeuser/myapp/root'
- );
+ 1;
- __PACKAGE__->setup;
-
sub end : Private {
my ( $self, $c ) = @_;
+
$c->stash->{template} ||= 'index.tt';
- $c->forward('MyApp::View::TT');
+ $c->forward( $c->view('TT') );
}
- sub view : Global {
- my ( $self, $c, $id ) = @_;
- $c->stash->{item} = MyApp::Model::CDBI::Foo->retrieve($id);
- }
-
- 1;
+We then create a new template file "root/index.tt" containing:
- # Then, in a TT template:
- The id is [% item.data %]
+ The Id's data is [% item.data %]
Models do not have to be part of your Catalyst application; you
can always call an outside module that serves as your Model:
# in a Controller
sub list : Local {
my ( $self, $c ) = @_;
+
$c->stash->{template} = 'list.tt';
- use Some::Outside::CDBI::Module;
- my @records = Some::Outside::CDBI::Module->retrieve_all;
+
+ use Some::Outside::DBIC::Module;
+ my @records = Some::Outside::DBIC::Module->search({
+ artist => 'sri',
+ });
+
$c->stash->{records} = \@records;
}
in a cron job), it's trivial to write a simple component in
Catalyst that slurps in an outside Model:
- package MyApp::Model::Catalog;
- use base qw/Catalyst::Base Some::Other::CDBI::Module::Catalog/;
+ package MyApp::Model::DB;
+ use base qw/Catalyst::Model::DBIC::Schema/;
+ __PACKAGE__->config(
+ schema_class => 'Some::DBIC::Schema',
+ connect_info => ['dbi:SQLite:foo.db', '', '', {AutoCommit=>1}];
+ );
1;
-and that's it! Now C<Some::Other::CDBI::Module::Catalog> is part of your
-Cat app as C<MyApp::Model::Catalog>.
+and that's it! Now C<Some::DBIC::Schema> is part of your
+Cat app as C<MyApp::Model::DB>.
=head4 Controllers