[Dbix-class] Schema::Loader and has_many
Brandon Black
blblack at gmail.com
Wed Mar 22 15:36:34 CET 2006
On 3/22/06, Jason Kohles <email at jasonkohles.com> wrote:
> On 3/22/06, Vsevolod (Simon) Ilyushchenko <simonf at cshl.edu> wrote:
> > Hi,
> >
> > With Brandon's help, I've created a simple example that uses
> > Schema::Loader, but I've hit a snag when I tried to use has_many.
> >
> > First, the loader was created in the file Test/Schema.pm, and my model
> > classes were in Test/M/Class.pm. The problem is that if I can't call
> > __PACKAGE__->has_many(...) inside Test/M/Class.pm, which inherits only
> > from Catalyst::Model::DBIC::Schema at the time the file is loaded.
> >
> > I've realized that the auto-loaded classes are called
> > Test/Schema/Class.pm, so I've renamed my schema to M.pm. However, this
> > caused other weird problems (I can give more details if necessary).
> >
> > What is the right way to specify relationships with Loader?
> >
> Normally the way you would do this is to build your schema classes
> separately, completely outside of Catalyst, like so...
>
> package Test::Schema;
> use base qw(DBIx::Class::Schema::Loader);
>
> __PACKAGE__->load_from_connection(
> connect_info => [ "DBI:Pg:dbname=whatever", "postgres", "password",
> { AutoCommit => 1 }],
> additional_classes => [qw(Some::More::Classes)],
> components => [qw(ResultSetManager)],
> relationships => 1,
> options => { AutoCommit => 1 },
> );
>
> You then specify additionial relationships in the Schema-related
> classes, rather than in the model...
>
> package Test::Schema::SomeClass;
> use base qw(DBIx::Class);
>
> __PACKAGE__->has_many(somethings => 'Test::Schema::SomeThing');
>
>
> (Note that I don't normally use the loader, so the options specific to
> it in the example may be screwy, but everything else is the way I do
> it in my own apps...)
>
> In your model, you then just specify which schema to use...
>
> package MyApp::Model::DBIC;
> use base qw(Catalyst::Model::DBIC::Schema);
>
> __PACKAGE__->config(
> schema_class => 'Test::Schema',
> connect_info => [ ... more connect info, if needed ... ],
> );
>
>
> The nice thing about doing it this way (with the Schema existing
> completely outside Catalyst, and the model just loading it) is that if
> you have other tools besides Catalyst that need to access the database
> (like admin scripts and cronjobs for example), they can just load up
> the schema and have the same level of access to the database.
>
Also, if you're using the Schema::Loader, the external table-class
add-on definitions don't really need the standard setup code. They
can just be like:
package Test::Schema::SomeClass;
__PACKAGE__->has_many(somethings => 'Test::Schema::SomeThing');
1;
Because by the time they are loaded, this class name already exists in
memory and already has the correct base classes and all.
-- Brandon
More information about the Dbix-class
mailing list