[Catalyst] Using model layer with DBIC::Schema

Fernan Aguero fernan at iib.unsam.edu.ar
Tue Jun 27 22:22:47 CEST 2006


+----[ Dr. Jennifer Nussbaum <bg271828 at yahoo.com> (27.Jun.2006 16:54):
|
|    Hi,

Hi

|    im just moving some work to DBIx::Class (which im really liking!), and
|    i have a
|    question about setting up my Schemas.
|    The docs for Catalyst::Plugin::DBIC::Schema talk a lot about setting
|    up your
|    DBIC classes in MyApp::Schema so you can use it on it's own, and then
|    creating MyApp::Model::FilmDB, which calls MyApp::Schema::Film as it's
|    Schema Class. So you then have Model::Film which is in Catalyst but
|    doesnt
|    really exists, it's a creation of the Schema.
|    i want to know the best way to set up a REAL Model::Film with DBIC.

If I understand what you want to do, what you want is
something like this:

CatalystApp/
  script/
  root/
  lib/
    Catalyst.pm
    Catalyst/
      Controller/
      View/
      Model/
        DB.pm -> references Catalyst::DB
      DB.pm -> this is Catalyst::DB
      DB/ -> here are all your DBIx classes 
        Users.pm
	Recipes.pm
	Tracks.pm
	Albums.pm
        Etcetera.pm


|    That is,
|    i have a Catalyst Controller that needs a model to do things relating
|    to CATALYST. i want my controller to pass $c to a model that can do a
|    bunch of
|    Catalyst related things, and IT calls my DBIC class. In other words i
|    want a
|    model layer in Catalyst that calls my fixed, not-Catalyst model in
|    MyApp::Schema.

I'm also new here so don't take my word for it, but I'm not
passing anything to my model ... at least the way I think of
it, I'm USING my model from within the controller:

in Controller/Discs.pm

sub search : Global {
 ( my $self, $context ) = @_;
 my $album = $context->model( 'DB::Albums' );
 my $search_string = $context->request->param( 'search_string' );
 my $resultset = $album->search_like( { album_name => "%$search_string%" });
 $context->stash->{albums} = $resultset;
 $context->stash->{template} = 'albums.tt';
}

|    Is there a recomended way of doing this with the Catalyst plugin 

You have to run your helper script application_create.pl 
to create a model for you, like this:

supposing you want to call your model 'DB'
./script/app_create.pl model DB DBIC 'dbi:mysql:dbname=mydb ...

this will generate the CatalystApp/lib/Catalyst/Model
directory and will put a DB.pm in there

| or a recomended way of doing it from scratch?

you can create the necessary directories by hand and write
your own DB.pm to put in Model/

package Catalyst::Model::DB;
use strict;
use base 'Catalyst::Model::DBIC::Schema';
__PACKAGE__->config(
 schema_class => 'Catalyst::DB',
 connect_info => [ 'dbi:mysql:dbname=', 'user', 'passwd' ]
 );
1;

that's it!

Of course you may want to put your DBIx classes and modules
elsewhere (here I've put them inside the 'lib' directory of
my catalyst app, at the same level of 'Controller', 'Model',
etc.), provided they're available for perl. In that case,
change the 'schema_class' in your Catalyst/Model/DB.pm to
point to it.

Hope this helps,

Fernan

|    Thank you!
|    Jen
|
+----]



More information about the Catalyst mailing list