[Maypole] Maypole model inheritance

Simon Flack sf@flacks.net
Sun, 12 Dec 2004 17:34:54 +0000


Dave Howorth wrote:
> Simon Flack wrote:
> 
>> Dave Howorth wrote:
>>
>>> Now it's morning, I see an obvious evil patch that's a variation of
>>> my experiment last night. Simply start with the original M-M-CDBI
>>> that *does* inherit from C-DBI so that all the C-DBI-* modules can
>>> be used. Then at the end of setup_database(), once the
>>> loader-determined C-DBI subclass is known, _remove_ C-DBI from
>>> M-M-CDBI's @ISA and substitute the C-DBI subclass.
>>
>>
>> Unfortunately, that'll break under mod_perl if you have one app using
>> a Mysql datasource and another using some other datasource such as Pg
>> or SQLite.
> 
> 
> A variant would be to leave C-DBI in place in M-M_CDBI's @ISA but push 
> it rather than unshift it onto the table subclasses' @ISA. I still 
> haven't looked into why an unshift was used ...  (See below why you 
> might want to do this).

If the model class isn't at the front of @ISA, then I can't do this:

sub MyModel::search {
     # override the standard CDBI search
     # ...
}


>> One solution is to remove Class::DBI entirely from M::M::CDBI's @ISA
>> and make your table classes inherit from a new base class. Consider
>> the following inheritance model:
>>
>> @MyApp::Model::Table::ISA = (MyApp::Model::_CDBI) 
>> @MyApp::Model::_CDBI::ISA = (M::M::CDBI, Class::DBI::$driver) 
>> @Maypole::Model::CDBI::ISA = (M::M::Base)
>>
>> M::M::CDBI can defer loading of Class::DBI::RetrieveAll, etc until
>> the new base class is created. And $r->config->model remains near the
>> front of your table classes' @ISA, where it belongs.
> 
> 
> The difficulty here is that it's C-DBI-Loader that makes 
> MyApp::Model::Table inherit directly from Class::DBI::$driver. So to 
> make this work, you have to rewrite C-DBI-Loader, I think? Which seems 
> odd, since the scenario (using C-DBI-Loader to set up the table classes 
> but also wanting to use other C-DBI-* modules) is pure C-DBI, no 
> Maypole, so there ought to be an existing solution out there somewhere.

M::M::CDBI could reset the table's @ISA and set up the appropriate
inheritance when it adopt()s the table class.

> OTOH, if you're willing to discard C-DBI-Loader, my auto-generated 
> application class doesn't suffer from this problem (It's MySQL only at 
> the moment, but it doesn't have the problem in principle :)
> 
> Also, mightn't there be another problem with your suggested solution if 
> any of the C-DBI-* modules initialize differently when connected to 
> different databases? The only safe place for them to be loaded would be 
> MyApp::Model::_CDBI (or the individual table classes).

Yep, that's what I was getting at. M::M::CDBI could load them into the
table class when they're adopted.

--simonflk