3 DBIx::Class::Manual::QuickStart - up and running with DBIC in 10 minutes
7 This document shows the minimum amount of code to make you a productive DBIC
8 user. It requires you to be familiar with just the basics of database
9 programming (what database tables, rows and columns are) and the basics of
10 Perl object-oriented programming (calling methods on an object instance).
11 It also helps if you already know a bit of SQL and how to connect to a
14 Follow along with the example database shipping with this distribution,
15 see directory F<examples/Schema>. This database is also used through-out the
16 rest of the documentation.
20 First, install DBIx::Class like you do with any other CPAN distribution.
21 See L<http://www.cpan.org/modules/INSTALL.html> and L<perlmodinstall>.
23 Then open the distribution in your shell and change to the subdirectory
24 mentioned earlier, the next command will download and unpack it:
26 $ perl -mCPAN -e'CPAN::Shell->look("DBIx::Class")'
27 DBIx-Class$ cd examples/Schema
31 DBIx-Class/examples/Schema$ echo .dump | sqlite3 db/example.db
33 You can also use a GUI database browser such as
34 L<SQLite Manager|https://addons.mozilla.org/firefox/addon/sqlite-manager>.
36 Have a look at the schema classes files in the subdirectory F<MyApp>. The
37 C<MyApp::Schema> class is the entry point for loading the other classes and
38 interacting with the database through DBIC and the C<Result> classes correspond
39 to the tables in the database. L<DBIx::Class::Manual::Example> shows how to
40 write all that Perl code. That is almost never necessary, though. Instead use
41 L<dbicdump> (part of the distribution C<DBIx-Class-Schema-Loader>) to
42 automatically create schema classes files from an existing database. The
43 chapter L</"Resetting the database"> below shows an example invocation.
45 =head2 Connecting to the database
47 A L<schema|DBIx::Class::Manual::Glossary/Schema> object represents the database.
49 use MyApp::Schema qw();
50 my $schema = MyApp::Schema->connect('dbi:SQLite:db/example.db');
52 The first four arguments are the same as for L<DBI/connect>.
54 =head2 Working with data
56 Almost all actions go through a
57 L<resultset|DBIx::Class::Manual::Glossary/ResultSet> object.
61 Via intermediate result objects:
63 my $artist_ma = $schema->resultset('Artist')->create({
64 name => 'Massive Attack',
66 my $cd_mezz = $artist_ma->create_related(cds => {
69 for ('Angel', 'Teardrop') {
70 $cd_mezz->create_related(tracks => {
75 Via relation accessors:
77 $schema->resultset('Artist')->create({
81 title => q{Kill 'Em All},
83 { title => 'Jump in the Fire' },
84 { title => 'Whiplash' },
90 { title => 'The Memory Remains' },
91 { title => 'The Unforgiven II' },
98 Columns that are not named are filled with default values. The value C<undef>
99 acts as a C<NULL> in the database.
101 See the chapter L</"Introspecting the schema classes"> below to find out where
102 the non-obvious source name strings such as C<Artist> and accessors such as
103 C<cds> and C<tracks> come from.
105 Set the environment variable C<DBI_TRACE='1|SQL'> to see the generated queries.
107 =head3 Retrieving data
111 my $artists_starting_with_m = $schema->resultset('Artist')->search(
113 name => { like => 'M%' }
117 Iterate over result objects of class C<MyApp::Schema::Result::Artist>.
118 L<Result|DBIx::Class::Manual::Glossary/Result> objects represent a row and
119 automatically get accessors for their column names.
121 for my $artist ($artists_starting_with_m->all) {
127 Change the release year of all CDs titled I<ReLoad>.
129 $schema->resultset('Cd')->search(
141 Removes all tracks titled I<Fuel> regardless of which CD the belong to.
143 $schema->resultset('Track')->search(
149 =head2 Introspecting the schema classes
151 This is useful for getting a feel for the naming of things in a REPL or during
152 explorative programming.
154 From the root to the details:
156 $schema->sources; # returns qw(Cd Track Artist)
157 $schema->source('Cd')->columns; # returns qw(cdid artist title year)
158 $schema->source('Cd')->relationships; # returns qw(artist tracks)
160 From a detail to the root:
162 $some_result->result_source; # returns appropriate source
163 $some_resultset->result_source;
164 $some_resultsource->schema; # returns appropriate schema
166 =head2 Resetting the database
168 # delete database file
169 DBIx-Class/examples/Schema$ rm -f db/example.db
171 # create database and set up tables from definition
172 DBIx-Class/examples/Schema$ sqlite3 db/example.db < db/example.sql
174 # fill them with data
175 DBIx-Class/examples/Schema$ perl ./insertdb.pl
177 # delete the schema classes files
178 DBIx-Class/examples/Schema$ rm -rf MyApp
180 # recreate schema classes files from database file
181 DBIx-Class/examples/Schema$ dbicdump \
182 -o dump_directory=. MyApp::Schema dbi:SQLite:db/example.db
184 =head2 Where to go next
186 If you want to exercise what you learned with a more complicated schema,
187 load L<Northwind|http://code.google.com/p/northwindextended/> into your
190 If you want to transfer your existing SQL knowledge, read
191 L<DBIx::Class::Manual::SQLHackers>.
193 Continue with L<DBIx::Class::Tutorial> and
194 L<DBIx::Class/"WHERE TO START READING">.