[Catalyst] Mapping Schema to Model in Catalyst?

dennis ddaupert at sbcglobal.net
Tue May 16 01:40:24 CEST 2006


On Sunday 14 May 2006 23:25, John wrote:

> I am sure seperating the Schema Class files from the
> Catalyst model directory is the right way to go, sine
> this give you a lot for flexibility.  Please let me
> know what you find out, because I am planning to write
> a more detailed version of this setup to put on the
> Wiki someplace and can use the early feedback.

I think I've found the answer to how schema is mapped to model in doc for 
Catalyst::Model::DBIC::Schema-0.13. (Unless this has been deprecated by 
now ;-) ) I've not actually gotten this to work quite yet, but I think it may 
just be missing a small something. Anyways, here's what I have based on the 
above:



#------------------------------------
Create the DBIx:Class schema
#------------------------------------
package Catapult::Schema::CatapultDB;

use warnings;
use strict;
use base qw/DBIx::Class::Schema/;

__PACKAGE__->load_classes(qw/Quotes/);

1;
#------------------------------------
Create a class for the table in the database
#------------------------------------
package Catapult::Schema::CatapultDB::Quotes;

use base qw/DBIx::Class/;

__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/);

1;
#------------------------------------
To expose it to Catalyst as a model...
#------------------------------------
package Catapult::Model::CatapultDB;

use warnings;
use strict;

use base qw/Catalyst::Model::DBIC::Schema/;

__PACKAGE__->config (
    schema_class => 'Catapult::Schema::CatapultDB',
    connect_info => [
                     'dbi::Pg:dbname=catapult',
                     'catapult',
                     '',
                    {
                     RaiseError => 1,
                     PrintError => 0,
                     ShowErrorStatement => 1,
                     Tracelevel => 0,
                    }]);

1;
#------------------------------------
Now in my controller:
#------------------------------------
sub list : Path('/text/quote/list_quote') {
  my ($self, $c) = @_;

  $c->stash->{quotes} = [$c->model('Quotes')->all];
  $c->stash->{template} = 'text/quote/list_quote.tt';
}
#------------------------------------

I know my DB is setup correctly, my previous code based on Kennedy's auth 
tutorial did at least retrieve the list of quotes in the quotes table.

Any thoughts?

/dennis





More information about the Catalyst mailing list