[Catalyst] Model--best practice help

Eden Cardim edencardim at gmail.com
Sun Oct 5 18:40:54 BST 2008


On Sun, Oct 5, 2008 at 2:03 PM, Dr. Jennifer Nussbaum
<bg271828 at yahoo.com> wrote:
> 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?

Create a class that's independent of Catalyst and glue it to your app
via Catalyst::Component::InstancePerContext:

package MyApp::Book;

use Moose;

has store => (isa => 'DBIx::Class::ResultSet', is => 'ro', required => 1);

sub foo { shift->store->search(etc...) }
sub bar { ... }
...

package MyApp::Model::Book;

use Moose;
extends 'Catalyst::Model';
with 'Catalyst::Component::InstancePerContext'

use MyApp::Book;

has store_model_name (isa => 'Str', is => 'ro', required => 1);

__PACKAGE__->config(store_model_name => 'MyAppDB::Book');

sub build_per_context_instance {
  my($self, $c) = @_;
  my $store = $c->model($self->store_model_name);
  return MyApp::Book->new(store => $store);
}

# in some distant controller:

$c->model('Book')->foo;
$c->model('Book')->bar; # runs on the same instance as the previous call

-- 
edenc.vox.com



More information about the Catalyst mailing list