]> git.decadent.org.uk Git - maypole.git/blob - lib/Maypole/Model/CDBI/Plain.pm
fd34a75c91d312bcb6437cca6c4dc6883d67e52d
[maypole.git] / lib / Maypole / Model / CDBI / Plain.pm
1 package Maypole::Model::CDBI::Plain;
2 use Maypole::Config;
3 use base 'Maypole::Model::CDBI';
4 use strict;
5
6 Maypole::Config->mk_accessors(qw(table_to_class));
7
8 =head1 NAME
9
10 Maypole::Model::CDBI::Plain - Class::DBI model without ::Loader
11
12 =head1 SYNOPSIS
13
14     package Foo;
15     use 'Maypole::Application';
16
17     Foo->config->model("Maypole::Model::CDBI::Plain");
18     Foo->setup([qw/ Foo::SomeTable Foo::Other::Table /]);
19
20 =head1 DESCRIPTION
21
22 This module allows you to use Maypole with previously set-up
23 L<Class::DBI> classes; simply call C<setup> with a list reference
24 of the classes you're going to use, and Maypole will work out the
25 tables and set up the inheritance relationships as normal.
26
27 =head1 METHODS
28
29 =head2 setup
30
31   This method is inherited from Maypole::Model::Base and calls setup_database,
32   which uses Class::DBI::Loader to create and load Class::DBI classes from
33   the given database schema.
34
35 =head2 setup_database
36
37   This method loads the model classes for the application
38
39 =cut
40
41
42
43 sub setup_database {
44     my ( $self, $config, $namespace, $classes ) = @_;
45     $config->{classes}        = $classes;
46     foreach my $class (@$classes) { $namespace->load_model_subclass($class); }
47     $namespace->model_classes_loaded(1);
48     $config->{table_to_class} = { map { $_->table => $_ } @$classes };
49     $config->{tables}         = [ keys %{ $config->{table_to_class} } ];
50 }
51
52 =head2 class_of
53
54   returns class for given table
55
56 =cut
57
58 sub class_of {
59     my ( $self, $r, $table ) = @_;
60     return $r->config->{table_to_class}->{$table};
61 }
62
63 =head2 adopt
64
65 This class method is passed the name of a model class that represensts a table
66 and allows the master model class to do any set-up required.
67
68 =cut
69
70 sub adopt {
71     my ( $self, $child ) = @_;
72     if ( my $col = $child->stringify_column ) {
73         $child->columns( Stringify => $col );
74     }
75 }
76
77 =head1 SEE ALSO
78
79 L<Maypole::Model::Base>
80
81 L<Maypole::Model::CDBI>
82
83 =cut
84
85
86 1;
87
88