]> git.decadent.org.uk Git - maypole.git/blob - lib/Maypole/Model/CDBI.pm
cc78879df3a2ac73316381147882fc458d0de11b
[maypole.git] / lib / Maypole / Model / CDBI.pm
1 package Maypole::Model::CDBI;
2 use strict;
3
4 =head1 NAME
5
6 Maypole::Model::CDBI - Model class based on Class::DBI
7
8 =head1 DESCRIPTION
9
10 This is a master model class which uses L<Class::DBI> to do all the hard
11 work of fetching rows and representing them as objects. It is a good
12 model to copy if you're replacing it with other database abstraction
13 modules.
14
15 It implements a base set of methods required for a Maypole Data Model.
16
17 It inherits accessor and helper methods from L<Maypole::Model::Base>.
18
19 When specified as the application model, it will use Class::DBI::Loader
20 to generate the model classes from the provided database. If you do not
21 wish to use this functionality, use L<Maypole::Model::CDBI::Plain> which
22 will instead use Class::DBI classes provided.
23
24 =cut
25
26 use base qw(Maypole::Model::CDBI::Base);
27 use Class::C3;
28 use Data::Dumper;
29 use Class::DBI::Loader;
30 use attributes ();
31
32 use Maypole::Model::CDBI::AsForm;
33 use Maypole::Model::CDBI::FromCGI;
34 use CGI::Untaint::Maypole;
35
36 =head2 Untainter
37
38 Set the class you use to untaint and validate form data
39 Note it must be of type CGI::Untaint::Maypole (takes $r arg) or CGI::Untaint
40
41 =cut
42
43 sub Untainter { 'CGI::Untaint::Maypole' };
44
45 =head2 add_model_superclass
46
47 Adds model as superclass to model classes (if necessary)
48
49 Inherited from Maypole::Model::CDBI::Base
50
51 =head1 Action Methods
52
53 Action methods are methods that are accessed through web (or other public) interface.
54
55 Inherited from L<Maypole::Model::CDBI::Base>
56
57 =head2 do_edit
58
59 If there is an object in C<$r-E<gt>objects>, then it should be edited
60 with the parameters in C<$r-E<gt>params>; otherwise, a new object should
61 be created with those parameters, and put back into C<$r-E<gt>objects>.
62 The template should be changed to C<view>, or C<edit> if there were any
63 errors. A hash of errors will be passed to the template.
64
65 =head2 do_delete
66
67 Inherited from Maypole::Model::CDBI::Base.
68
69 This action deletes records
70
71 =head2 do_search
72
73 Inherited from Maypole::Model::CDBI::Base.
74
75 This action method searches for database records.
76
77 =head2 list
78
79 Inherited from Maypole::Model::CDBI::Base.
80
81 The C<list> method fills C<$r-E<gt>objects> with all of the
82 objects in the class. The results are paged using a pager.
83
84 =head1 Helper Methods
85
86 =head2 setup
87
88   This method is inherited from Maypole::Model::Base and calls setup_database,
89   which uses Class::DBI::Loader to create and load Class::DBI classes from
90   the given database schema.
91
92 =cut
93
94 =head2 setup_database
95
96 The $opts argument is a hashref of options.  The "options" key is a hashref of
97 Database connection options . Other keys may be various Loader arguments or
98 flags.  It has this form:
99  {
100    # DB connection options
101    options { AutoCommit => 1 , ... },
102    # Loader args
103    relationships => 1,
104    ...
105  }
106
107 =cut
108
109 sub setup_database {
110     my ( $class, $config, $namespace, $dsn, $u, $p, $opts ) = @_;
111     $dsn  ||= $config->dsn;
112     $u    ||= $config->user;
113     $p    ||= $config->pass;
114     $opts ||= $config->opts;
115     $config->dsn($dsn);
116     warn "No DSN set in config" unless $dsn;
117     $config->loader || $config->loader(
118         Class::DBI::Loader->new(
119             namespace => $namespace,
120             dsn       => $dsn,
121             user      => $u,
122             password  => $p,
123             %$opts,
124         )
125     );
126     $config->{classes} = [ $config->{loader}->classes ];
127     $config->{tables}  = [ $config->{loader}->tables ];
128
129     my @table_class = map { $_ . " => " . $config->{loader}->_table2class($_) } @{ $config->{tables} };
130     warn( 'Loaded tables to classes: ' . join ', ', @table_class )
131       if $namespace->debug;
132 }
133
134 =head2 class_of
135
136   returns class for given table
137
138 =cut
139
140 sub class_of {
141     my ( $self, $r, $table ) = @_;
142     return $r->config->loader->_table2class($table); # why not find_class ?
143 }
144
145
146 =head1 SEE ALSO
147
148 L<Maypole>, L<Maypole::Model::CDBI::Base>.
149
150 =head1 AUTHOR
151
152 Maypole is currently maintained by Aaron Trevena.
153
154 =head1 AUTHOR EMERITUS
155
156 Simon Cozens, C<simon#cpan.org>
157
158 Simon Flack maintained Maypole from 2.05 to 2.09
159
160 Sebastian Riedel, C<sri#oook.de> maintained Maypole from 1.99_01 to 2.04
161
162 =head1 LICENSE
163
164 You may distribute this code under the same terms as Perl itself.
165
166 =cut
167
168 1;