described schema better, and added note about when you want to use it
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Manual / Intro.pod
CommitLineData
076652e8 1=head1 Introduction.
2
3So, you are bored with SQL, and want a native perl interface for your classes?
4Or you've been doing this for a while with L<Class::DBI>, and think there's
5a better way? You've come to the right place. Let's look at how you can set
6and use your first native DBIx::Class tree.
7
8First we'll see how you can set up your classes yourself. If you want them
9to be auto-discovered, just skip to the next section, which shows you how
10to use DBIx::Class::Loader.
11
12=head2 Setting it up manually
13
14First, you'll need a base class. It should inherit from DBIx::Class
15like this:
16
17 package MyApp::DB
18 use base qw/DBIx::Class/;
19
20You will also want to load some of L<DBIx::Class>'s components.
21L<DBIx::Class::Core> provides a good basic set. In addition you'll
22have to use either L<DBIx::Class::Schema> or L<DBIx::Class::DB> We'll
429bd4f1 23use DB in this introduction, since it involves less magic. Schema is
24mostly useful if you want to use multiple database connections.
076652e8 25
26 __PACKAGE__->load_components(qw/Core DB/);
27
28If you want serial/auto-incremental primary keys, you'll need to add
29the apropriate component for your db as well, for example
30
31 __PACKAGE__->load_components(qw/Core DB PK::Auto::SQLite/);
32
33Once you've loaded the components, it's time to set up your connection:
34
35 __PACKAGE__->connection('dbi:SQLite:/home/me/myapp/my.db');
36
37This method is similar to the normal L<DBI>, and can take user/pass/dbi
38attribute hash as well as the dsn.
39
40With that out of the way, we can define our first table class:
41
42 package MyApp::DB::Frob
43
44 use base qw/MyApp::DB/;
45
46Then we specify which table it uses,
47
48 __PACKAGE__->table('frob');
49
50and specify which columns it has.
51
52 __PACKAGE__->add_columns(qw/id foo bar/);
53
54This will automatically create accessors for each of the columns, so that
55you can read/update the values in rows you've retrieved.
56
57Also, you need to tell it which column is the primary key:
58
59 __PACKAGE__->set_primary_key('id');
60
61If you have multiple primary keys, just pass a list instead.
62
63That's pretty much all you need for a basic setup. If you have more advanced
64needs like using more than 1 database connections for the same class, see
65L<DBIx::Class::Schema>.
66
67=head2 Using L<DBIx::Class::Loader>.
68
69This is an additional class, and not part of the DBIx::Class distribution.
70Like L<Class::DBI::Loader>, it inspects your database, and automatically
71creates classes for all the tables in your database. Here's a simple setup:
72
73 package MyApp::DB;
74
75 use DBIx::Class::Loader;
76
77 my $loader=DBIx::Class::Loader->new(
78 dsn => 'dbi:SQLite:/home/me/myapp/my.db',
79 namespace => 'MyApp::DB');
80 1;
81
82This should be equivalent to the manual in the section above.
83L<DBIx::Class::Loader> takes lots of other options. For more information,
84consult the reference documentation.
85
86=head2 Basic Usage
87
88Once you've defined the basic classes, you can start interacting with your
89database. The simplest way to get a column is by primary key:
90
91 my $frob=MyApp::DB::Frob->find(14);
92
93This will run a select with id=14 in the WHERE clause, and return an instance
94of MyApp::DB::Frob that represents this row. Once you have that row, you can
95access and update columns
96
97 my $val=$frob->bar;
98 $frob->bar(14);
99
100or if you prefer, you can use the set_column/get_column accessors instead
101of the autogenerated accessors based on your column names.
102
103Just like with L<Class::DBI>, you do an 'update' to commit your changes
104to the database:
105
106 $frob->update;
107
108If needed, you can drop your local changes instead like this:
109
110 $frob->discard_changes if $frob->is_changed;
111
112As you can see, is_changed allows you to check if there are local changes to
113your object.
114
115=head2 Adding and removing rows.
116
117To make a new row, and put it into the database, you can use the 'create'
118method from L<DBIx::Class::Row>
119
120 my $new_thingie=MyApp::DB::Frob->create({
121 foo=>'homer',
122 bar=>'bart' });
123
124likewise, you can remove if from the database like this:
125
126 $new_thingie->delete();
127
128or even without retrieving first. This operation takes the same kind of
129arguments as a search.
130
131 MyApp::DB::Frob->delete({foo=>'bart'});
132
133=head2 Finding your objects.
134
135DBIx::Class provides a few different ways to retrieve data from your database.
136The simplest looks something like this:
137
138 $rs=MyApp::DB::Frob->search(foo=>'bart');
139
140note that all the search methods return a recordset in scalar context or
141a list containing all the elements in list context.
142
143We also provide a handy shortcut for doing a like search:
144
145 $rs=MyApp::DB::Frob->search_like(foo=>'bar%');
146
147Or you can provide your own handmade WHERE clause, like
148
149 $rs=MyApp::DB::Frob->search_literal('foo=?','bart');
150
151The other way to provide more complex queries, is to provide a
152L<SQL::Abstract> construct to search:
153
154 $rs=MyApp::DB::Frob->search({
155 bar=>{'>' => 10 },
156 foo=>{'!=','bart'},
157 id => [1,14,15,65,43]
158 });
159
160The search can also be modifyed by passing another hash with attributes:
161
162 $rs=MyApp::DB::Frob->search( {foo=>'bart'},
163 { page=>1, rows=>2, order_by=>'bar' } );
164
165For a complete overview over the available attributes, see
166L<DBIx::Class::ResultSet>
167
168=cut