1 package Maypole::Model::CDBI;
6 Maypole::Model::CDBI - Model class based on Class::DBI
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
15 It implements a base set of methods required for a Maypole Data Model.
17 It inherits accessor and helper methods from L<Maypole::Model::Base>.
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.
26 use base qw(Maypole::Model::CDBI::Base);
28 use Class::DBI::Loader;
31 use Maypole::Model::CDBI::AsForm;
32 use Maypole::Model::CDBI::FromCGI;
33 use CGI::Untaint::Maypole;
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
42 sub Untainter { 'CGI::Untaint::Maypole' };
44 =head2 add_model_superclass
46 Adds model as superclass to model classes (if necessary)
48 Inherited from Maypole::Model::CDBI::Base
52 Action methods are methods that are accessed through web (or other public) interface.
54 Inherited from L<Maypole::Model::CDBI::Base>
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.
66 Inherited from Maypole::Model::CDBI::Base.
68 This action deletes records
72 Inherited from Maypole::Model::CDBI::Base.
74 This action method searches for database records.
78 Inherited from Maypole::Model::CDBI::Base.
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.
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.
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:
99 # DB connection options
100 options { AutoCommit => 1 , ... },
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;
115 warn "No DSN set in config" unless $dsn;
116 $config->loader || $config->loader(
117 Class::DBI::Loader->new(
118 namespace => $namespace,
125 $config->{classes} = [ $config->{loader}->classes ];
126 $config->{tables} = [ $config->{loader}->tables ];
128 my @table_class = map { $_ . " => " . $config->{loader}->_table2class($_) } @{ $config->{tables} };
129 warn( 'Loaded tables to classes: ' . join ', ', @table_class )
130 if $namespace->debug;
135 returns class for given table
140 my ( $self, $r, $table ) = @_;
141 return $r->config->loader->_table2class($table); # why not find_class ?
147 L<Maypole>, L<Maypole::Model::CDBI::Base>.
151 Maypole is currently maintained by Aaron Trevena.
153 =head1 AUTHOR EMERITUS
155 Simon Cozens, C<simon#cpan.org>
157 Simon Flack maintained Maypole from 2.05 to 2.09
159 Sebastian Riedel, C<sri#oook.de> maintained Maypole from 1.99_01 to 2.04
163 You may distribute this code under the same terms as Perl itself.