[Catalyst] Model--best practice help

J. Shirley jshirley at gmail.com
Sun Oct 5 18:24:56 BST 2008


On Sun, Oct 5, 2008 at 10:03 AM, Dr. Jennifer Nussbaum
<bg271828 at yahoo.com> wrote:
> Im going through my code, and trying to really think about how i can make it work well. Its a mess, i am realising, and i need advice on the best way
> to clean it up.
>
> Its clear that there are things in my Model that should be in a Controller; that i dont need help with.
>
> My problem is that i have two different Models, and dont know how they should be combined.
>
> In particular, im using DBIC, so i have a setup where i have MyApp::Schema::Main, which only looks like:
>
> package MyApp::Schema::Main;
> use base qw/DBIx::Class::Schema/;
>
> __PACKAGE__->load_classes(qw/Book Author/);
>
> 1;
>
> (the config stuff is in a separate config file:)
>
> <Model::MyAppDB>
>        schema_class MyApp::Schema::Main
>        connect_info #######
> </Model::MyAppDB>
>
> Then i'll have MyApp::Schema::Main::Book of whatever that looks like
>
> package MyApp::Schema::Main::Book;
>
> use base qw/DBIx::Class/;
>
> __PACKAGE__->load_components(qw/PK::Auto Core/);
> (etc., other DBIC stuff)
>
> The problem is i also have a MyApp::Model::Book, and ive usually put non-DBIC things in there, like if i have a "get_results" method thats
> specific to my Cat app and not appropriate for the schema, ill put it there. That starts like
>
> package MyApp::Model::Book;
> use strict;
> use warnings;
> use base 'Catalyst::Model';
>
> So ive got these two model classes, one i call with $c->model('MyAppDB::Book') and the other with $c->model('Book'). The problem
> is, i have some things that are Cat specific and i dont want them in my schema class (becaues i use this in non-Cat apps), but i dont want to
> have to have two separate models that i call by different names at
> different times.
>
> Whats the way im supposed to be setting these up?
>
> Thanks!
>
> Jen
>
>
>


Without seeing a tremendous amount of your code it is hard to answer,
but it sounds like you are either at a point where it would be
advantageous to learn Moose, or start doing some multiple inheritance
hacks to get things working.

The simple approach, which is much less "great" than using Moose, is
to put your App specific (the Catalyst specific, as you phrased it)
methods in a class, and then in your model class:

package MyApp::Model::Everything;

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

__PACKAGE__->config( ... );

1;

Then you can do $c->model('Everything')->foo_method( ... );
(foo_method defined in Model::Book), and still have
$c->model('Everything::SchemaClass')->search(...);

In foo_method, $self will be Catalyst::Model::DBIC::Schema, so you can
do $self->schema->resultset('SchemaClass')->search(...); etc.

But really... I'd just learn Moose, it'll make things easier and
cleaner in the long run.

-J



More information about the Catalyst mailing list