X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FManual%2FIntro.pod;h=737848f00e6ad0da8a4bbcb001376a75af48913b;hb=bd93520f2fc007efd8d0062f44b611cb00072b7d;hp=10cb50e7836937fa1ac5f00a017caacd419a9e63;hpb=429bd4f15b0ea80aef96873a9db11801538eb3ee;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/Manual/Intro.pod b/lib/DBIx/Class/Manual/Intro.pod index 10cb50e..737848f 100644 --- a/lib/DBIx/Class/Manual/Intro.pod +++ b/lib/DBIx/Class/Manual/Intro.pod @@ -1,168 +1,329 @@ -=head1 Introduction. +=head1 NAME -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. +DBIx::Class::Manual::Intro - Introduction to DBIx::Class -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. +=head1 INTRODUCTION + +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 L. =head2 Setting it up manually -First, you'll need a base class. It should inherit from DBIx::Class -like this: +First, you should create your base schema class, which inherits from +L: + + package My::Schema; + use base qw/DBIx::Class::Schema/; + +In this class you load your result_source ("table", "model") classes, which +we will define later, using the load_classes() method. You can specify which +classes to load manually: + + # load My::Schema::Album and My::Schema::Artist + __PACKAGE__->load_classes(qw/ Album Artist /); + +Or load classes by namespace: - package MyApp::DB + # load My::Schema::Album, My::Schema::Artist and My::OtherSchema::LinerNotes + __PACKAGE__->load_classes( + { + 'My::Schema' => [qw/ Album Artist /], + 'My::OtherSchema' => [qw/ LinerNotes /] + } + ); + +Or let your schema class load all classes in its namespace automatically: + + # load My::Schema::* + __PACKAGE__->load_classes(); + +Next, create each of the classes you want to load as specified above: + + package My::Schema::Album; 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. +Load any components required by each class with the load_components() method. +This should consist of "Core" plus any additional components you want to use. +For example, if you want serial/auto-incrementing primary keys: + + __PACKAGE__->load_components(qw/ PK::Auto Core /); + +C is supported for many databases; see +L for more information. - __PACKAGE__->load_components(qw/Core DB/); +Set the table for your class: -If you want serial/auto-incremental primary keys, you'll need to add -the apropriate component for your db as well, for example + __PACKAGE__->table('album'); - __PACKAGE__->load_components(qw/Core DB PK::Auto::SQLite/); +Add columns to your class: -Once you've loaded the components, it's time to set up your connection: + __PACKAGE__->add_columns(qw/ albumid artist title /); - __PACKAGE__->connection('dbi:SQLite:/home/me/myapp/my.db'); +Each column can also be set up with its own accessor, data_type and other +pieces of information that it may be useful to have, just pass C +a hash such as: -This method is similar to the normal L, and can take user/pass/dbi -attribute hash as well as the dsn. + __PACKAGE__->add_columns(albumid => + { accessor => 'album', + data_type => 'integer', + size => 16, + is_nullable => 0, + is_auto_increment => 1, + default_value => '', + }, + artist => + { data_type => 'integer', + size => 16, + is_nullable => 0, + is_auto_increment => 0, + default_value => '', + }, + title => + { data_type => 'varchar', + size => 256, + is_nullable => 0, + is_auto_increment => 0, + default_value => '', + } + ); -With that out of the way, we can define our first table class: +Most of this data isn't yet used directly by DBIx::Class, but various related +modules such as L make use of it. Also it allows you +to create your database tables from your Schema, instead of the other way +around. See L for details. - package MyApp::DB::Frob +See L for more details of the possible column +attributes. - use base qw/MyApp::DB/; +Accessors are created for each column automatically, so My::Schema::Album will +have albumid() (or album(), when using the accessor), artist() and title() +methods. -Then we specify which table it uses, +Define a primary key for your class: - __PACKAGE__->table('frob'); + __PACKAGE__->set_primary_key('albumid'); -and specify which columns it has. +If you have a multi-column primary key, just pass a list instead: - __PACKAGE__->add_columns(qw/id foo bar/); + __PACKAGE__->set_primary_key( qw/ albumid artistid / ); -This will automatically create accessors for each of the columns, so that -you can read/update the values in rows you've retrieved. +Define relationships that the class has with any other classes by using +either C to describe a column which contains an ID of another +table, or C to make a predefined accessor for fetching objects +that contain this tables foreign key in one of their columns: -Also, you need to tell it which column is the primary key: + __PACKAGE__->has_many('albums', 'My::Schema::Artist', 'album_id'); - __PACKAGE__->set_primary_key('id'); +More information about the various types of relationships available, and +how you can design your own, can be found in L. -If you have multiple primary keys, 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. +=head2 Using L -=head2 Using L. +This is an external module, 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: -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: + package My::Schema; + use base qw/DBIx::Class::Schema::Loader/; - package MyApp::DB; - - use DBIx::Class::Loader; + __PACKAGE__->load_from_connection( + connect_info = [ 'dbi:SQLite:/home/me/myapp/my.db' ] + ); - my $loader=DBIx::Class::Loader->new( - 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 Connecting -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: +L already contains the connection info for the +database, so to get started all you need to do is create an instance of your +class: - my $frob=MyApp::DB::Frob->find(14); + my $schema = My::Schema->new(); -This will run a select with id=14 in the WHERE clause, and return an instance -of MyApp::DB::Frob that represents this row. Once you have that row, you can -access and update columns +To connect to your manually created Schema, you also need to provide the +connection details: - my $val=$frob->bar; - $frob->bar(14); + my $schema = My::Schema->connect('dbi:SQLite:/home/me/myapp/my.db'); -or if you prefer, you can use the set_column/get_column accessors instead -of the autogenerated accessors based on your column names. +You can create as many different schema instances as you need. So if you have +a second database you want to access: -Just like with L, you do an 'update' to commit your changes -to the database: + my $other_schema = My::Schema->connect( $dsn, $user, $password, $attrs ); - $frob->update; +Note that L does not cache connections for you. If you +use multiple connections, you need to do this manually. -If needed, you can drop your local changes instead like this: +To execute some sql statements on every connect you can pass them to your schema after the connect: - $frob->discard_changes if $frob->is_changed; + $schema->storage->on_connect_do(\@on_connect_sql_statments); -As you can see, is_changed allows you to check if there are local changes to -your object. +=head2 Basic usage -=head2 Adding and removing rows. +Once you've defined the basic classes, either manually or using +L, you can start interacting with your database. -To make a new row, and put it into the database, you can use the 'create' -method from L +To access your database using your $schema object, you can fetch a L +representing each of your tables by calling the ->resultset method. - my $new_thingie=MyApp::DB::Frob->create({ - foo=>'homer', - bar=>'bart' }); +The simplest way to get a record is by primary key: -likewise, you can remove if from the database like this: + my $album = $schema->resultset('Album')->find(14); - $new_thingie->delete(); +This will run a C