1DBIx::Class(3)        User Contributed Perl Documentation       DBIx::Class(3)
2
3
4

NAME

6       DBIx::Class - Extensible and flexible object <-> relational mapper.
7

WHERE TO START READING

9       See DBIx::Class::Manual::DocMap for an overview of the exhaustive
10       documentation.  To get the most out of DBIx::Class with the least
11       confusion it is strongly recommended to read (at the very least) the
12       Manuals in the order presented there.
13

GETTING HELP/SUPPORT

15       Due to the sheer size of its problem domain, DBIx::Class is a
16       relatively complex framework. After you start using DBIx::Class
17       questions will inevitably arise. If you are stuck with a problem or
18       have doubts about a particular approach do not hesitate to contact us
19       via any of the following options (the list is sorted by "fastest
20       response time"):
21
22       ·   IRC: irc.perl.org#dbix-class
23
24       ·   Mailing list:
25           <http://lists.scsys.co.uk/mailman/listinfo/dbix-class>
26
27       ·   RT Bug Tracker:
28           <https://rt.cpan.org/NoAuth/Bugs.html?Dist=DBIx-Class>
29
30       ·   Twitter: <https://www.twitter.com/dbix_class>
31
32       ·   Web Site: <http://www.dbix-class.org/>
33

SYNOPSIS

35       For the very impatient: DBIx::Class::Manual::QuickStart
36
37       This code in the next step can be generated automatically from an
38       existing database, see dbicdump from the distribution
39       "DBIx-Class-Schema-Loader".
40
41   Schema classes preparation
42       Create a schema class called MyApp/Schema.pm:
43
44         package MyApp::Schema;
45         use base qw/DBIx::Class::Schema/;
46
47         __PACKAGE__->load_namespaces();
48
49         1;
50
51       Create a result class to represent artists, who have many CDs, in
52       MyApp/Schema/Result/Artist.pm:
53
54       See DBIx::Class::ResultSource for docs on defining result classes.
55
56         package MyApp::Schema::Result::Artist;
57         use base qw/DBIx::Class::Core/;
58
59         __PACKAGE__->table('artist');
60         __PACKAGE__->add_columns(qw/ artistid name /);
61         __PACKAGE__->set_primary_key('artistid');
62         __PACKAGE__->has_many(cds => 'MyApp::Schema::Result::CD', 'artistid');
63
64         1;
65
66       A result class to represent a CD, which belongs to an artist, in
67       MyApp/Schema/Result/CD.pm:
68
69         package MyApp::Schema::Result::CD;
70         use base qw/DBIx::Class::Core/;
71
72         __PACKAGE__->load_components(qw/InflateColumn::DateTime/);
73         __PACKAGE__->table('cd');
74         __PACKAGE__->add_columns(qw/ cdid artistid title year /);
75         __PACKAGE__->set_primary_key('cdid');
76         __PACKAGE__->belongs_to(artist => 'MyApp::Schema::Result::Artist', 'artistid');
77
78         1;
79
80   API usage
81       Then you can use these classes in your application's code:
82
83         # Connect to your database.
84         use MyApp::Schema;
85         my $schema = MyApp::Schema->connect($dbi_dsn, $user, $pass, \%dbi_params);
86
87         # Query for all artists and put them in an array,
88         # or retrieve them as a result set object.
89         # $schema->resultset returns a DBIx::Class::ResultSet
90         my @all_artists = $schema->resultset('Artist')->all;
91         my $all_artists_rs = $schema->resultset('Artist');
92
93         # Output all artists names
94         # $artist here is a DBIx::Class::Row, which has accessors
95         # for all its columns. Rows are also subclasses of your Result class.
96         foreach $artist (@all_artists) {
97           print $artist->name, "\n";
98         }
99
100         # Create a result set to search for artists.
101         # This does not query the DB.
102         my $johns_rs = $schema->resultset('Artist')->search(
103           # Build your WHERE using an SQL::Abstract structure:
104           { name => { like => 'John%' } }
105         );
106
107         # Execute a joined query to get the cds.
108         my @all_john_cds = $johns_rs->search_related('cds')->all;
109
110         # Fetch the next available row.
111         my $first_john = $johns_rs->next;
112
113         # Specify ORDER BY on the query.
114         my $first_john_cds_by_title_rs = $first_john->cds(
115           undef,
116           { order_by => 'title' }
117         );
118
119         # Create a result set that will fetch the artist data
120         # at the same time as it fetches CDs, using only one query.
121         my $millennium_cds_rs = $schema->resultset('CD')->search(
122           { year => 2000 },
123           { prefetch => 'artist' }
124         );
125
126         my $cd = $millennium_cds_rs->next; # SELECT ... FROM cds JOIN artists ...
127         my $cd_artist_name = $cd->artist->name; # Already has the data so no 2nd query
128
129         # new() makes a Result object but doesn't insert it into the DB.
130         # create() is the same as new() then insert().
131         my $new_cd = $schema->resultset('CD')->new({ title => 'Spoon' });
132         $new_cd->artist($cd->artist);
133         $new_cd->insert; # Auto-increment primary key filled in after INSERT
134         $new_cd->title('Fork');
135
136         $schema->txn_do(sub { $new_cd->update }); # Runs the update in a transaction
137
138         # change the year of all the millennium CDs at once
139         $millennium_cds_rs->update({ year => 2002 });
140

DESCRIPTION

142       This is an SQL to OO mapper with an object API inspired by Class::DBI
143       (with a compatibility layer as a springboard for porting) and a
144       resultset API that allows abstract encapsulation of database
145       operations. It aims to make representing queries in your code as perl-
146       ish as possible while still providing access to as many of the
147       capabilities of the database as possible, including retrieving related
148       records from multiple tables in a single query, "JOIN", "LEFT JOIN",
149       "COUNT", "DISTINCT", "GROUP BY", "ORDER BY" and "HAVING" support.
150
151       DBIx::Class can handle multi-column primary and foreign keys, complex
152       queries and database-level paging, and does its best to only query the
153       database in order to return something you've directly asked for. If a
154       resultset is used as an iterator it only fetches rows off the statement
155       handle as requested in order to minimise memory usage. It has auto-
156       increment support for SQLite, MySQL, PostgreSQL, Oracle, SQL Server and
157       DB2 and is known to be used in production on at least the first four,
158       and is fork- and thread-safe out of the box (although your DBD may not
159       be).
160
161       This project is still under rapid development, so large new features
162       may be marked experimental - such APIs are still usable but may have
163       edge bugs.  Failing test cases are always welcome and point releases
164       are put out rapidly as bugs are found and fixed.
165
166       We do our best to maintain full backwards compatibility for published
167       APIs, since DBIx::Class is used in production in many organisations,
168       and even backwards incompatible changes to non-published APIs will be
169       fixed if they're reported and doing so doesn't cost the codebase
170       anything.
171
172       The test suite is quite substantial, and several developer releases are
173       generally made to CPAN before the branch for the next release is merged
174       back to trunk for a major release.
175

HOW TO CONTRIBUTE

177       Contributions are always welcome, in all usable forms (we especially
178       welcome documentation improvements). The delivery methods include git-
179       or unified-diff formatted patches, GitHub pull requests, or plain bug
180       reports either via RT or the Mailing list. Do not hesitate to get in
181       touch with any further questions you may have.
182
183       This project is maintained in a git repository. The code and related
184       tools are accessible at the following locations:
185
186       ·   Current git repository: <https://github.com/Perl5/DBIx-Class>
187
188       ·   Travis-CI log: <https://travis-ci.org/Perl5/DBIx-Class/branches>
189

AUTHORS

191       Even though a large portion of the source appears to be written by just
192       a handful of people, this library continues to remain a collaborative
193       effort - perhaps one of the most successful such projects on CPAN
194       <http://cpan.org>.  It is important to remember that ideas do not
195       always result in a direct code contribution, but deserve
196       acknowledgement just the same. Time and time again the seemingly most
197       insignificant questions and suggestions have been shown to catalyze
198       monumental improvements in consistency, accuracy and performance.
199
200       The canonical source of authors and their details is the AUTHORS file
201       at the root of this distribution (or repository). The canonical source
202       of per-line authorship is the git repository history itself.
203
205       Copyright (c) 2005 by mst, castaway, ribasushi, and other DBIx::Class
206       "AUTHORS" as listed above and in AUTHORS.
207
208       This library is free software and may be distributed under the same
209       terms as perl5 itself. See LICENSE for the complete licensing terms.
210
211
212
213perl v5.30.1                      2020-01-29                    DBIx::Class(3)
Impressum