[Catalyst] Adding relationships to loaded CDBI models

Danijel Milicevic Danijel.Milicevic at rawmode.org
Thu Jun 16 19:20:51 CEST 2005

Hello Christopher,

Am Donnerstag, den 16.06.2005, 13:15 -0400 schrieb Christopher Heschong:
> So a model based on Catalyst::Model::CDBI loads up all my tables and
> columns, sweet.
> But then how do I extend one of those auto-generated classes with extra
> relationships.
> For instance, in my MyApp::M::CDBI I would like to do this:
> package MyApp::M::CDBI;
> use base 'Catalyst::Model::CDBI';
> __PACKAGE__->config( ... );
> MyApp::M::CDBI::Table->has_a (
> 	date	=> 'Class::Date',
> 	inflate	=> 'new',
> 	deflate	=> 'epoch'
> );
> 1;
> But at the time that MyApp::M::CDBI is loaded, the MyApp::M::CDBI::Table
> class doesn't exist yet.  I could use Catalyst::Model::CDBI::Plain and
> define everything myself, but is there any way to take the ease of
> C::M::CDBI and extend it a little?

All you have to do is supply a dbi path to the myapp_create.pl helper
script, it'll create classes for your tables. An example:

gabb at spalato:~/MyApp$ sqlite3 /tmp/myapp.db < myapp.sql
gabb at spalato:~/MyApp$ script/myapp_create.pl model CDBI CDBI dbi:SQLite:/tmp/myapp.db
created "/home/gabbana/MyApp/script/../lib/MyApp/M/CDBI.pm"
created "/home/gabbana/MyApp/script/../lib/MyApp/M/CDBI"
created "/home/gabbana/MyApp/script/../lib/MyApp/M/CDBI/Item.pm"

Then you add the relationships in MyApp::M::CDBI::Item, and _NOT_ in CDBI.pm.
Re-running the helper script won't overwrite existing modules btw, so you can run it everytime you update your SQL.


More information about the Catalyst mailing list