[Catalyst] Accessing schema in non-DBIC-model

Matt S Trout dbix-class at trout.me.uk
Sun Jul 20 01:36:24 BST 2008

On Tue, Jul 01, 2008 at 02:49:37PM +0200, Tobias Kremer wrote:
> I'm using Catalyst::Model::DBIC::Schema to make my DBIC schema available as
> model in my Catalyst application. Now I'd like to create a custom model (e.g.
> MyApp::Model::Custom) which during the server start fetches and calculates some
> stuff from the database via my DBIC schema and keeps it in the object to speed
> up retrieval in my Cat app. I want to do:
> my $rs = $c->model( 'Custom' )->find_by_foo( $foo )
> and get back a (e.g. MyApp::Schema::Foo) ResultSet with stuff fetched during
> server start that fits the condition $foo.
> My problem is that I can't think of an elegant way to access the initialized
> schema from within the models new or COMPONENT method (which is called during
> server start when the model is set up). I tried this:
>     my( $class, $app, $args ) = @_;
>     $app->model( 'Schema::Foo' )->result_source->schema ...
> }
> But, of course, $app->model('Schema::Foo') isn't fully initialized at that time.
> Any hint is greatly appreciated!

Catalyst initializes less-namespaced names first.

So I suspect if you called this MyApp::Model::Schema::Precache then
$c->model('Schema')->resultset('Foo') would be fine.

      Matt S Trout       Need help with your Catalyst or DBIx::Class project?
   Technical Director                    http://www.shadowcat.co.uk/catalyst/
 Shadowcat Systems Ltd.  Want a managed development or deployment platform?
http://chainsawblues.vox.com/            http://www.shadowcat.co.uk/servers/

More information about the Catalyst mailing list