Accessing schema in non-DBIC-model

View: New views
2 Messages — Rating Filter:   Alert me  

Accessing schema in non-DBIC-model

by Tobias Kremer-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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:

sub COMPONENT {
    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!

--Tobias

_______________________________________________
List: Catalyst@...
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@.../
Dev site: http://dev.catalyst.perl.org/

Re: Accessing schema in non-DBIC-model

by Matt S Trout-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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:
>
> sub COMPONENT {
>     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/

_______________________________________________
List: Catalyst@...
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst@.../
Dev site: http://dev.catalyst.perl.org/
LightInTheBox - Buy quality products at wholesale price