[Catalyst] Configuring individual models via config file with DBIx::Class:Schema

Tomas Doran bobtfish at bobtfish.net
Wed Mar 24 00:49:58 GMT 2010

On 22 Mar 2010, at 19:15, Alan Schwartz wrote:

> I have an application using DBIx::Class::Schema. In the application  
> config file, I have:
> <Model::DB>
> schema_class MyApp::Schema
> <connect_info>
> ...various necessary stuff here...
> </connect_info>
> </Model::DB>
> <Controller::One>
> someattribute value
> </Controller::One>
> <DB::Two>
> anotherattribute anothervalue
> </DB::Two>
> The first two of these blocks do what I expect -- my models
> get build from the Schema/Result files, I get connections, etc.
> And in Controller/One.pm, I can access $self->{someattribute}

To start with, don't do that!

Make a _real_ attribute, e.g.

has someattribute => ( is => 'ro' );

and then call the proper accessor, e.g.


> But I can't for the life of my figure out how I can get  
> anotherattribute
> into the config for an individual model based on the schema.

The problem you are having is that your mental model is broken. Your  
entire DBIC schema is _one_ model. There is not one model per table in  
your DB.

The DBIC model does some trickery as a shortcut for you - so that you  
can say $c->model('DB::Person') rather than $c->model('DB')- 
 >resultset('Person').. However everything under the top level model  
class is owned and controlled by that class, it's not a separate model  
and so the normal rules don't really apply. :(

> That is, I'd like methods in DB::Two objects to have access
> to $self->{anotherattribute}
> In addition to the above, I have tried:
> * Variations on the config block (<myapp::Model::DB::Two>,  
> <Model::DB::Two>,
> <Two>, even <MyApp::Schema::Result::Two>)
> * Putting that block within the <Model::DB> block, again with all   
> those variations

If you just put the attribute directly on your Schema (rather than a  
specific resultset), then what you want will (I think, off the top of  
my head having not looked) it'll all just work.

You can trivially get back to the schema from the resultset, and (if  
it's more convienient) add a delegate method in the resultset to get  
the attribute from the schema.

> I haven't found the answer so far in searching this mailing list
> or the web. Is this possible?  What am I missing here?
> Thanks in advance!

Hop on irc into #catalyst or #dbix-class and people will be extremely  
happy to lead you through sorting out what you want to achieve (whilst  
also being architecturally correct) if you're prepared to write a  
sentence or two (perhaps even an entire paragraph?) for the docs to  
explain this to the next person who comes along?


More information about the Catalyst mailing list