[Dbix-class] RFC: DBIx::Class 0.05 Schemas + Catalyst?
Jess Robinson
castaway at desert-island.demon.co.uk
Wed Feb 1 14:39:00 CET 2006
I'm currently using a DBIC setup which supports $c->model('MyModelBase')
and even $c->model('Schema::MyClass');
Using msts new COMPONENT and ACCEPT_CONTEXT thingys for Catalyst, it works
like this:
MyApp::Model::MyModelBase uses My::Schema, which is elsewhere, and
contains:
sub COMPONENT
{
my ($class, $c, $config) = @_;
my $schema =
My::Schema->compose_connection('MyApp::Model::Schema',
'dbi:mydb:dbname',, 'user', 'passw');
return $schema;
}
which makes $c->model('MyModelBase') work..
and the MyApp::Model::Schema::MyClass contains:
sub ACCEPT_CONTEXT
{
my ($self, $c) = @_;
return $c->model('MyModelBase')->resultset('MyClass');
}
which makes $c->model('Schema::MyClass') work..
So maybe we just need a Helper that creates these..
Jess
On Tue, 31 Jan 2006, Brandon Black wrote:
> It seems to me that there should be a Catalyst::Model::DBIC::Schema,
> which makes a $c->model('FooDB') for a given Schema + connection info.
>
> Unlike the previous Catalyst::Model::DBIC, which only used ::Loader,
> this Model should work for any Schema, whether it was a manual one, or
> one defined by ::Schema::Loader. The Schema itself should be
> elsewhere (not in the Catalyst Model/ directory. If you really want
> it to be wrapped up in your Catalyst app, stick it in
> lib/MyApp/Schema.pm or something).
>
> I hacked up a Catalyst::Model::DBIC::Schema today and got it to appear
> to work correctly. My model class ends up looking like:
>
> -----------------------------------
> package MyApp::M::MyDB;
>
> use strict;
> use base 'Catalyst::Model::DBIC::Schema';
>
> __PACKAGE__->config(
> schema_class => 'My::Schema',
> dsn => 'dbi:Pg:dbname=mydb',
> user => 'postgres',
> password => '',
> options => { AutoCommit => 1 },
> );
>
> 1;
> --------------------------------------
>
> Where My::Schema isa DBIx::Class::Schema defined elsewhere outside the
> model directory (like lib/My/Schema.pm). Note that this allows you to
> have multiple $c->model('Foo'), $c->model('Bar'), etc, which use the
> same Schema-class but different connection information.
>
> My draft Catalyst::Model::DBIC::Schema is attached as an attachment.
>
> The big problem with the approach I used to make this, is that all
> access from within controllers turns into the idiom:
>
> $c->model('MyDB')->schema->resultset('Foo')->search(...).
>
> I'd really like to get the "->schema" step out of there, but I didn't
> see any obvious (to me) way of getting the Catalyst Model instance to
> simultaneously be both a Catalyst::Base and a schema object, so I just
> put the schema object in a ->schema accessor.
>
> Thoughts on all of this, anyone?
>
> -- Brandon
>
More information about the Dbix-class
mailing list