NAME
Class::DBI::Loader::mysql::Grok - Build Quality Table Relationships Automatically
SYNOPSIS
use Class::DBI::Loader; # optional
use Class::DBI::Loader::mysql::Grok;
my $loader = Class::DBI::Loader->new(
...
namespace => "Music",
relationships => 1,
);
my $class = $loader->find_class('artist'); # $class => Music::Artist
my $artist = $class->retrieve(1);
for my $cd ($artist->cds) {
print $cd->artist->name,"\n";
print $cd->reldate->ymd,"\n"; # a Time::Piece object
}
# etc ...
DESCRIPTION
If you name your tables and columns using some common sense rules, there's no need for you to do any work to have proper db abstraction. The following examples mostly follow the Class::DBI perldoc. To see where they differ (immaterially), see the test script and the accompanying SQL.
The kinds of relationships which are created include:
- has_a
-
In the example above, the cd table contains a column which matches the name of another table: artist. In this case, Music::Cd objects have a has_a relationship with Music::Artist. As a result, you can call $cd->artist->name, etc.
- has_many
-
Similar to the has_a example above, the fact that the cd table contains a column which matches the name of another table means that Music::Artist objects have a has_many relationship with Music::CD. As a result, you can call $artist->cds->next->title, etc.
- has_many mapping
-
When we're working with a mapping table like Music::StyleRef in the Class::DBI perldoc, which maps a many-to-many relationship, the mapping table name must =~ /_ref$/i, and the columns in that table must be named after the tables to which they refer.
- might_have
-
The liner_notes table's primary key is named 'cd'. Since that's so, and the table name (liner_notes) !~ /_ref$/i: Music::Cd->might_have(liner_notes_notes => Music::LinerNotes => 'notes');
- Time::Piece support
-
While not a multi-table relationship, Time::Piece support is included for date, time, datetime, and timestamp types.
EXPORT
None by default, but it does redefine the _relationships routine in Class::DBI::Loader::mysql.
SEE ALSO
Class::DBI Class::DBI::Loader, Class::DBI::Loader::mysql, Time::Piece
AUTHOR
James Tolley, <james@bitperfect.com>
COPYRIGHT AND LICENSE
Copyright (C) 2005 by James Tolley
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.6 or, at your option, any later version of Perl 5 you may have available.
2 POD Errors
The following errors were encountered while parsing the POD:
- Around line 123:
'=item' outside of any '=over'
- Around line 155:
You forgot a '=back' before '=head2'