[Catalyst] Adding relationships to loaded CDBI models

samwyse samwyse at gmail.com
Thu Jun 16 22:33:11 CEST 2005


On 6/16/05, Danijel Milicevic <Danijel.Milicevic at rawmode.org> wrote:
> Am Donnerstag, den 16.06.2005, 13:52 -0400 schrieb Rob Kinyon:
> > This brings up an interesting point - what if you're not using SQLite,
> > but instead are using MySQL/Oracle/Sybase/etc. Some of those dbi:
> > strings can get quite long. What's a best practice for handling that?
> 
> I'm not too sure if I can call it best practice already, but most of us
> (the guys in #catalyst) do supply those "quite long" strings nonetheless
> to the helper method and export the configuration laters to a YAML file.
> Quite useful if you use a simple and lightweight DB like SQLite during
> development and move laters to MySQL/Oracle/PostgreSQL in production.

Also, not all databases support the discovery of relationships, so I'm
using this in my version of the example package Hops::M::CDBI:
    use Class::DBI::Loader::Relationship;

and a bit later in the 'new' method:
    # Unless relationships are already defined in the DB schema...
    unless (__PACKAGE__->config->{relationships}) {
        # Define relationships between the tables.
        $self->loader->relationship($_) for (
            "a brewery produces beers",
            "a style defines beers",
            "a pub has beers on handpumps",
        );
    }

Class::DBI::Loader::Relationship looks at simple pseudo-English
sentences and attempts to deduce 'has_a' and 'has_many' relationships
by looking for the plural and singular forms of the table names.  If
you use it with tables that define foreign key relationships,
$self->loader->relationship will complain, but in that case the helper
script will have set 'relationships' in the config hash to 1; thus the
'unless' statement.



More information about the Catalyst mailing list