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       ·   RT Bug Tracker:
23           <https://rt.cpan.org/Public/Dist/Display.html?Name=DBIx-Class>
24
25       ·   Email: <mailto:bug-DBIx-Class@rt.cpan.org>
26
27       ·   Twitter:
28           <https://twitter.com/intent/tweet?text=%40ribasushi%20%23DBIC>
29

SYNOPSIS

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

DESCRIPTION

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

HOW TO CONTRIBUTE

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

AUTHORS

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