[Catalyst] More complex Class::DBI::Sweet usage?

Toby Corkindale tjc at wintrmute.net
Fri Jul 1 12:22:16 CEST 2005


On Fri, Jul 01, 2005 at 12:07:32PM +0200, Danijel Milicevic wrote:
> Am Freitag, den 01.07.2005, 10:36 +0100 schrieb Toby Corkindale:
> > *delurk*
> > In Catalyst, where is the preferred place to setup custom queries for
> > autogenerated (via C::M::CDBI) classes?
> > In a package named exactly the same, so you can do __PACKAGE__->set_sql(), or
> > in a different package (ie. via C::M::CDBI::Foo->set_sql()) which must be
> > run after the setup stage of C::M::CDBI)?
> 
> In order for Catalyst to pick up your models you should place them in
> the MyApp::M:: namespace. You can do that manually for non-cdbi models
> or just use the helper script and a existing database:
> 
> gabbana at spalato:~$ MyApp/script/myapp_create.pl model CDBI CDBI
> dbi:SQLite:/home/gabbana/MyApp/myapp.db
> created "/home/gabbana/MyApp/script/../lib/MyApp/M/CDBI"
> created "/home/gabbana/MyApp/script/../lib/MyApp/M/CDBI/Cart.pm"
> created "/home/gabbana/MyApp/script/../lib/MyApp/M/CDBI/Customer.pm"
> created "/home/gabbana/MyApp/script/../lib/MyApp/M/CDBI/Product.pm"
> ...
> 
> Now just edit MyApp::M::CDBI::Cart for example and add your
> __PACKAGE__->set_sql() statements.


Ah, but this doesn't quite work for me - it seems like the CDBI functions
don't yet exist at the time the set_sql() routines are called, ie:

We see that the tables are all loaded, including the stores table:
[Fri Jul 01 11:16:41 2005] [error] [client 127.0.0.1] [Fri Jul  1 11:16:41
2005] [catalyst] [debug] Loaded tables "addresses admin_groups
admin_groups_rights admin_rights admin_rights_list admins admins_to_countries
borked_countries countries countries_to_sections data_requests
data_source_types data_sources diesel_regions email_content email_queue
email_types file_upload_info localised_forms member_types members
members_changes modified_info postcard_submission_info sections sent_email
sessions store_types stores stores_to_sections stores_to_store_types
validation_image validation_methods"

But the next line shows us failing when the Stores.pm package is loaded:
[Fri Jul 01 11:16:41 2005] [error] [client 127.0.0.1] Couldn't load
"DieselCatalyst::M::CDBI::Stores", "Can't locate object method "set_sql" via
package "DieselCatalyst::Model::CDBI::Stores" at
/home/tobyc/svn/DieselCatalyst/lib/DieselCatalyst/M/CDBI/Stores.pm line 11.

If I remove the offending set_sql() statement, then we load alright, and
amidst the debug messages is this line, where C::M::CDBI tells us it has
automatically created the appropriate class magically:
[Fri Jul 01 11:20:17 2005] [error] [client 127.0.0.1] | DieselCatalyst::M::CDBI::Stores   


Do I need to do something akin to Maypole's setup()-then-require trick?
(Presumably in a much more elegant manner though :)


Cheers,
Toby

-- 
Turning and turning in the widening gyre/The falcon cannot hear the falconer;
Things fall apart, the centre cannot hold/Mere anarchy is loosed upon the world
(gpg --keyserver www.co.uk.pgp.net --recv-key 897E5FF3)



More information about the Catalyst mailing list