X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FManual%2FIntro.pod;h=f288db6eb86710578c68a392fddd6bb06d22d7ae;hb=6d1bf0a9d5c6d11bd70b7d6938403530faeebc2d;hp=556c5527c142735d6e3cd891b0a8974c45071ce1;hpb=dfeba824c1e575d8629ca472c5fc5c28ef51b532;p=dbsrgits%2FDBIx-Class-Historic.git diff --git a/lib/DBIx/Class/Manual/Intro.pod b/lib/DBIx/Class/Manual/Intro.pod index 556c552..f288db6 100644 --- a/lib/DBIx/Class/Manual/Intro.pod +++ b/lib/DBIx/Class/Manual/Intro.pod @@ -2,51 +2,55 @@ DBIx::Class::Manual::Intro - Introduction to DBIx::Class -=head1 Introduction. +=head1 INTRODUCTION -So, you are bored with SQL, and want a native perl interface for your classes? -Or you've been doing this for a while with L, and think there's -a better way? You've come to the right place. Let's look at how you can set -and use your first native DBIx::Class tree. +So, you are bored with SQL, and want a native Perl interface for your +database? Or you've been doing this for a while with L, +and think there's a better way? You've come to the right place. +Let's look at how you can set and use your first native L +tree. -First we'll see how you can set up your classes yourself. If you want them -to be auto-discovered, just skip to the next section, which shows you how -to use DBIx::Class::Loader. +First we'll see how you can set up your classes yourself. If you want +them to be auto-discovered, just skip to the next section, which shows +you how to use L. =head2 Setting it up manually -First, you'll need a base class. It should inherit from DBIx::Class -like this: +First, you'll need a base class. It should inherit from +L like this: - package MyApp::DB + package MyApp::DB; use base qw/DBIx::Class/; -You will also want to load some of L's components. -L provides a good basic set. In addition you'll -have to use either L or L We'll -use DB in this introduction, since it involves less magic. Schema is -mostly useful if you want to use multiple database connections. +You will also want to load some of the L components. +L provides a good starter set. In addition you'll +have to use either L or L. +We'll use C in this introduction, since it involves less magic. +C is mostly useful if you want to use multiple database +connections. __PACKAGE__->load_components(qw/Core DB/); -If you want serial/auto-incremental primary keys, you'll need to add -the apropriate component for your db as well, for example. The -PK::Auto::* modules help L keep up with newly generated -keys in auto increment database fields. +If you want serial/auto-incrementing primary keys, you should use the +L component for your database. For example, if +you're using SQLite add C to the list: __PACKAGE__->load_components(qw/PK::Auto::SQLite Core DB/); -Once you've loaded the components, it's time to set up your connection: +C classes exist for many databases; see +L for more information. + +Once you've loaded the components, it's time to set up your +connection: __PACKAGE__->connection('dbi:SQLite:/home/me/myapp/my.db'); -This method is similar to the normal L, and can take user/pass/dbi -attribute hash as well as the dsn. +This method is similar to the normal L C method, and can +take username, password, and L attribute hash as well as the DSN. With that out of the way, we can define our first table class: package MyApp::DB::Album; - use base qw/MyApp::DB/; Then we specify which table it uses, @@ -55,130 +59,189 @@ Then we specify which table it uses, and specify which columns it has. - __PACKAGE__->add_columns(qw/albumID artist title label year/); + __PACKAGE__->add_columns(qw/albumid artist title label year/); -This will automatically create accessors for each of the columns, so that -you can read/update the values in rows you've retrieved. +This will automatically create accessors for each of the columns, so +that you can read/update the values in rows you've retrieved. Also, you need to tell it which column is the primary key: - __PACKAGE__->set_primary_key('albumID'); + __PACKAGE__->set_primary_key('albumid'); -If you have multiple primary keys, just pass a list instead. +If you have a primary key composed of multiple columns, just pass a +list instead. -That's pretty much all you need for a basic setup. If you have more advanced -needs like using more than 1 database connections for the same class, see -L. +That's pretty much all you need for a basic setup. If you have more +advanced needs like using more than one database connection for the +same class, see L. -=head2 Using L. +=head2 Using L -This is an additional class, and not part of the DBIx::Class distribution. -Like L, it inspects your database, and automatically -creates classes for all the tables in your database. Here's a simple setup: +This is an additional class, and not part of the L +distribution. Like L, it inspects your database, +and automatically creates classes for all the tables in your database. +Here's a simple setup: package MyApp::DB; - use DBIx::Class::Loader; my $loader = DBIx::Class::Loader->new( - dsn => 'dbi:SQLite:/home/me/myapp/my.db', - namespace => 'MyApp::DB'); + dsn => 'dbi:SQLite:/home/me/myapp/my.db', + namespace => 'MyApp::DB' + ); + 1; -This should be equivalent to the manual in the section above. -L takes lots of other options. For more information, -consult the reference documentation. +This should be equivalent to the manual setup in the section above. +L takes lots of other options. For more +information, consult its documentation. -=head2 Basic Usage +=head2 Basic usage -Once you've defined the basic classes, you can start interacting with your -database. The simplest way to get a column is by primary key: +Once you've defined the basic classes, either manually or using +L, you can start interacting with your database. +The simplest way to get a record is by primary key: - $albumID = 14; - $album = MyApp::DB::Album->find($albumID); + my $album = MyApp::DB::Album->find(14); -This will run a select with albumID=14 in the WHERE clause, and return an instance -of MyApp::DB::Artist that represents this row. Once you have that row, you can -access and update columns +This will run a C