[Catalyst] Mapping Schema to Model in Catalyst?

dennis ddaupert at sbcglobal.net
Sun May 14 00:27:04 CEST 2006


Hello,

I'm trying to figure out how to get the DBIx::Class schema object into my 
controllers in Catalyst.

Basically, the documentation says:
1) Create a schema class
2) Create a table class for each table.
3) Then you can use these classes in your application's code; eg,

my $schema = DB::Main->connect($dbi_dsn, $user, $pass, \%dbi_params);
And then you call methods on the schema object, and everything's hunkie dorey.

The example is based on a cgi application. But in Catalyst, the connection 
method in all the examples I've seen is separated from the controller code. 
So how do I get the schema object into my controllers? Use stash? FedEX? 
Email?

I'm trying to follow Kennedy's excellent Authentication And Authorization 
Tutorial, and I've gotten partway along (see code at bottom)

In my controller class, this works to grab a list of records in quotes table:

  $c->stash->{quotes} = [$c->model('CatapultDB::Quotes')->all];

(but I don't see how this maps to the schema stuff; I probably should mention 
that I have OO dyslexia.)

I've tried to retrieve one record using similar code, but I'm gonna lose my 
hair. This (one of many, many such silly attempts) certainly doesn't work:

$c->stash->{quotes} = [$c->model('CatapultDB::Quotes')->find($id)];

Then after retrieving the record, I want to edit it and update the db, and 
then go on and do some other tricks, and so on and so on.

/dennis

-----------------------------------------------
I have this schema class:
-----------------------------------------------
package Catapult::Model::CatapultDB;
use strict;
use base 'Catalyst::Model::DBIC::Schema';
__PACKAGE__->config(
    schema_class => 'CatapultDB',
    connect_info => [
        'dbi:Pg:dbname=catapult',
        'catapult',
        '',
        { AutoCommit => 1 },        
    ],
);
-----------------------------------------------
Quotes class:
-----------------------------------------------
package CatapultDB::Quotes;
use base qw/DBIx::Class/;
# Load required DBIC stuff
__PACKAGE__->load_components(qw/PK::Auto::Pg Core/);

# Set the table name
__PACKAGE__->table('quotes');
# Set columns in table
__PACKAGE__->add_columns(qw/id quote author/);
# Set the primary key for the table
__PACKAGE__->set_primary_key(qw/id/);
-----------------------------------------------
Controller class:
-----------------------------------------------
package Catapult::Controller::Text::Quote;
sub list : Path('/text/quote/list_quote') {
  my ($self, $c) = @_;
  $c->stash->{quotes} = [$c->model('CatapultDB::Quotes')->all];
  $c->stash->{template} = 'text/quote/list_quote.tt';
}
sub edit : Path('/text/quote/edit_quote') {
  my ( $self, $c ) = @_;

<buncha stuff>
# make sure the record is there
my $id = $c->request->params->{id};
$c->stash->{quotes} = [$c->model('CatapultDB::Quotes')->find($id)];
<more stuff>
# update the record, by some mysterious means...
}
-----------------------------------------------



More information about the Catalyst mailing list