On 11/9/05, Kieren Diment <[EMAIL PROTECTED]> wrote:
> To start refactoring my application, I started by refactoring BeerDB
> and wrote it up. The updated pod is on the wiki. The new section is
> below. I would appreciate comments or suggestions.
>
> A couple of questions though.
>
> I note from the documentation in Maypole::Model::CDBI::Plain that
> (for the BeerDB) we're asked to create our model classes in
> BeerDB::Beer, BeerDB::Style etc. What If I don't want to do that?
> How do I configure CDBI::Plain/Maypole to look for say the classes qw/
> Beer Beer::Brewery Beer::Style/ etc?
As long as your relationships code refers to the new names, and as
long as you explicitly load these modules via 'use', that part will
work fine.
> My brief experiment showed me that the untaint statements don't work
> if they're moved into the model classes, so for example the statement
>
> BeerDB::Brewery->untaint_columns( printable => [qw/name notes url/]);
>
> in Brewery.pm doesn't work (the error message is something like:
>
<snip error output>
It's because your model classes aren't inheriting from the appropriate
base class. See below. (This is also why you're getting errors with
Exported methods).
<snip>
>
> =head2 The database driver code code
>
> =over
>
> =item DBI.pm
>
> package BeerDB::DBI;
> use base 'Class::DBI';
> BeerDB::DBI->connection('dbi:mysql:beer', 'root', '');
> 1;
Instead of inheriting from CDBI, this should inherit from
Maypole::Model::CDBI::Plain (which, in turn, inherits from CDBI).
>
> =item Beer.pm
>
> package BeerDB::Beer;
> use base 'BeerDB::DBI';
It seems weird, but in 2.11 you won't need to make these classes
inherit from anything! Maypole will set that up. However, in 2.10, you
have a problem, which is that in order to support the Exported
attribute, your code has to already inherit from Maypole::Model::Base
when it's compiled. If you make Beer::DBI inherit from
Maypole::Model::CDBI::Plain, then this code will work. You'll end up
with BeerDB::DBI appearing twice in each class's @ISA, but just
pretend that's OK, it won't affect anything, and is fixed in 2.11.
> BeerDB::Beer->table('beer');
> BeerDB::Beer->columns(All => qw/brewery style name url score price/);
> BeerDB::Beer->has_a(brewery => 'BeerDB::Brewery');
> BeerDB::Beer->has_a(style => 'BeerDB::Style');
> BeerDB::Beer->has_many(beers => [BeerDB::Handpump => 'beer']);
> BeerDB::Beer->has_many(pubs => [BeerDB::Handpump => 'pub']);
> 1;
>
<snip lots of code>
> So that's quite a lot of code (for a trivial Maypole application).
> Now we need to wrap it all up in a driver class fro Maypole: this
> lives (according to my scheme) in /usr/local/src/beer/lib
>
> =over
>
> =item BeerDB.pm
>
> package BeerDB;
> use base 'Maypole::Application';
Just say use 'Maypole::Application'; Probably doesn't make any
difference to whether things work or not, but if you examine the
inheritance, it'll be a bit simpler. Maypole::Application only needs
to be there at the birth of your application, it's not part of it.
(Well, in 2.10, it sticks around, doing nothing, but in 2.11 it's
better behaved and departs the scene).
> use BeerDB::Beer;
> use BeerDB::Brewery;
> use BeerDB::Handpump;
> use BeerDB::Pub;
> use BeerDB::Style;
This is fine, but in 2.11, you won't need to load these here, they'll
get loaded during setup().
> BeerDB->config->model('Maypole::Model::CDBI::Plain');
No, the model should be your custom model, Beer::DBI, which should
inherit from Mp::M::CDBI::Plain.
> BeerDB->setup([qw/BeerDB::Beer BeerDB::Brewery BeerDB::Handpump
> BeerDB::Pub Beer
It's a bad habit to call setup() so early. It's fine for basic apps,
but as soon as you start adding plugins, they'll want access to config
data, and they want it during setup(). Get in the habit of putting
setup() after building all the config settings.
<snip more config>
HTH,
d.
-------------------------------------------------------
SF.Net email is sponsored by:
Tame your development challenges with Apache's Geronimo App Server. Download
it for free - -and be entered to win a 42" plasma tv or your very own
Sony(tm)PSP. Click here to play: http://sourceforge.net/geronimo.php
_______________________________________________
Maypole-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/maypole-users