[Dbix-class] Schema::Loader and has_many

Jason Kohles email at jasonkohles.com
Wed Mar 22 14:58:59 CET 2006


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.

--
Jason Kohles
email at jasonkohles.com - http://www.jasonkohles.com/
"A witty saying proves nothing."  -- Voltaire



More information about the Dbix-class mailing list