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