-package MyDatabase::Main;
+package MyApp::Schema;
use warnings;
use strict;
-package MyDatabase::Main::Result::Artist;
+package MyApp::Schema::Result::Artist;
use warnings;
use strict;
__PACKAGE__->set_primary_key('artistid');
-__PACKAGE__->has_many('cds' => 'MyDatabase::Main::Result::Cd');
+__PACKAGE__->has_many('cds' => 'MyApp::Schema::Result::Cd');
1;
-package MyDatabase::Main::Result::Cd;
+package MyApp::Schema::Result::Cd;
use warnings;
use strict;
__PACKAGE__->set_primary_key('cdid');
-__PACKAGE__->belongs_to('artist' => 'MyDatabase::Main::Result::Artist');
-__PACKAGE__->has_many('tracks' => 'MyDatabase::Main::Result::Track');
+__PACKAGE__->belongs_to('artist' => 'MyApp::Schema::Result::Artist');
+__PACKAGE__->has_many('tracks' => 'MyApp::Schema::Result::Track');
1;
-package MyDatabase::Main::Result::Track;
+package MyApp::Schema::Result::Track;
use warnings;
use strict;
__PACKAGE__->set_primary_key('trackid');
-__PACKAGE__->belongs_to('cd' => 'MyDatabase::Main::Result::Cd');
+__PACKAGE__->belongs_to('cd' => 'MyApp::Schema::Result::Cd');
1;
use strict;
use warnings;
-use MyDatabase::Main;
+use MyApp::Schema;
-my $schema = MyDatabase::Main->connect('dbi:SQLite:db/example.db');
+my $schema = MyApp::Schema->connect('dbi:SQLite:db/example.db');
# here's some of the sql that is going to be generated by the schema
# INSERT INTO artist VALUES (NULL,'Michael Jackson');
use warnings;
use strict;
-use MyDatabase::Main;
+use MyApp::Schema;
-my $schema = MyDatabase::Main->connect('dbi:SQLite:db/example.db');
+my $schema = MyApp::Schema->connect('dbi:SQLite:db/example.db');
# for other DSNs, e.g. MySql, see the perldoc for the relevant dbd
# driver, e.g perldoc L<DBD::mysql>.
To accomplish this one only needs to specify the DB schema name in the table
declaration, like so...
- package MyDatabase::Main::Artist;
+ package MyApp::Schema::Result::Artist;
use base qw/DBIx::Class::Core/;
__PACKAGE__->table('database1.artist'); # will use "database1.artist" in FROM clause
__PACKAGE__->add_columns(qw/ artist_id name /);
__PACKAGE__->set_primary_key('artist_id');
- __PACKAGE__->has_many('cds' => 'MyDatabase::Main::Cd');
+ __PACKAGE__->has_many('cds' => 'MyApp::Schema::Result::Cd');
1;
L<connection|DBIx::Class::Schama/connection> method in your Schema class and
building a renaming facility, like so:
- package MyDatabase::Schema;
+ package MyApp::Schema;
use Moose;
extends 'DBIx::Class::Schema';
L<connection|DBIx::Class::Schama/connect>, as follows:
my $schema
- = MyDatabase::Schema->connect(
+ = MyApp::Schema->connect(
$dsn,
$user,
$pass,
Now create some more directories:
- mkdir MyDatabase
- mkdir MyDatabase/Main
- mkdir MyDatabase/Main/Result
- mkdir MyDatabase/Main/ResultSet
+ mkdir MyApp
+ mkdir MyApp/Schema
+ mkdir MyApp/Schema/Result
+ mkdir MyApp/Schema/ResultSet
Then, create the following DBIx::Class::Schema classes:
-MyDatabase/Main.pm:
+MyApp/Schema.pm:
- package MyDatabase::Main;
+ package MyApp::Schema;
use base qw/DBIx::Class::Schema/;
__PACKAGE__->load_namespaces;
1;
-MyDatabase/Main/Result/Artist.pm:
+MyApp/Schema/Result/Artist.pm:
- package MyDatabase::Main::Result::Artist;
+ package MyApp::Schema::Result::Artist;
use base qw/DBIx::Class::Core/;
__PACKAGE__->table('artist');
__PACKAGE__->add_columns(qw/ artistid name /);
__PACKAGE__->set_primary_key('artistid');
- __PACKAGE__->has_many('cds' => 'MyDatabase::Main::Result::Cd');
+ __PACKAGE__->has_many('cds' => 'MyApp::Schema::Result::Cd');
1;
-MyDatabase/Main/Result/Cd.pm:
+MyApp/Schema/Result/Cd.pm:
- package MyDatabase::Main::Result::Cd;
+ package MyApp::Schema::Result::Cd;
use base qw/DBIx::Class::Core/;
__PACKAGE__->load_components(qw/InflateColumn::DateTime/);
__PACKAGE__->table('cd');
__PACKAGE__->add_columns(qw/ cdid artist title/);
__PACKAGE__->set_primary_key('cdid');
- __PACKAGE__->belongs_to('artist' => 'MyDatabase::Main::Result::Artist');
- __PACKAGE__->has_many('tracks' => 'MyDatabase::Main::Result::Track');
+ __PACKAGE__->belongs_to('artist' => 'MyApp::Schema::Result::Artist');
+ __PACKAGE__->has_many('tracks' => 'MyApp::Schema::Result::Track');
1;
-MyDatabase/Main/Result/Track.pm:
+MyApp/Schema/Result/Track.pm:
- package MyDatabase::Main::Result::Track;
+ package MyApp::Schema::Result::Track;
use base qw/DBIx::Class::Core/;
__PACKAGE__->table('track');
__PACKAGE__->add_columns(qw/ trackid cd title /);
__PACKAGE__->set_primary_key('trackid');
- __PACKAGE__->belongs_to('cd' => 'MyDatabase::Main::Result::Cd');
+ __PACKAGE__->belongs_to('cd' => 'MyApp::Schema::Result::Cd');
1;
use strict;
use warnings;
- use MyDatabase::Main;
+ use MyApp::Schema;
- my $schema = MyDatabase::Main->connect('dbi:SQLite:db/example.db');
+ my $schema = MyApp::Schema->connect('dbi:SQLite:db/example.db');
# here's some of the SQL that is going to be generated by the schema
# INSERT INTO artist VALUES (NULL,'Michael Jackson');
use strict;
use warnings;
- use MyDatabase::Main;
+ use MyApp::Schema;
- my $schema = MyDatabase::Main->connect('dbi:SQLite:db/example.db');
+ my $schema = MyApp::Schema->connect('dbi:SQLite:db/example.db');
# for other DSNs, e.g. MySQL, see the perldoc for the relevant dbd
# driver, e.g perldoc L<DBD::mysql>.
directory F<t/examples/Schema>.
With these scripts we're relying on @INC looking in the current
-working directory. You may want to add the MyDatabase namespaces to
+working directory. You may want to add the MyApp namespaces to
@INC in a different way when it comes to deployment.
The F<testdb.pl> script is an excellent start for testing your database
model.
This example uses L<DBIx::Class::Schema/load_namespaces> to load in the
-appropriate L<Row|DBIx::Class::Row> classes from the MyDatabase::Main::Result namespace,
-and any required resultset classes from the MyDatabase::Main::ResultSet
+appropriate L<Row|DBIx::Class::Row> classes from the MyApp::Schema::Result namespace,
+and any required resultset classes from the MyApp::Schema::ResultSet
namespace (although we created the directory in the directions above we
did not add, or need to add, any resultset classes).
If you are using L<Catalyst>, then you can use the helper that comes with
L<Catalyst::Model::DBIC::Schema>:
- $ script/myapp_create.pl model MyDB DBIC::Schema MyDB::Schema \
+ $ script/myapp_create.pl model MyModel DBIC::Schema MyApp::Schema \
create=static moniker_map='{ foo => "FOO" }' dbi:SQLite:./myapp.db \
on_connect_do='PRAGMA foreign_keys=ON' quote_char='"'
You can also use a GUI database browser such as
L<SQLite Manager|https://addons.mozilla.org/firefox/addon/sqlite-manager>.
-Have a look at the schema classes files in the subdirectory F<MyDatabase>. The
-C<MyDatabase::Main> class is the entry point for loading the other classes and
+Have a look at the schema classes files in the subdirectory F<MyApp>. The
+C<MyApp::Schema> class is the entry point for loading the other classes and
interacting with the database through DBIC and the C<Result> classes correspond
to the tables in the database. L<DBIx::Class::Manual::Example> shows how to
write all that Perl code. That is almost never necessary, though. Instead use
A L<schema|DBIx::Class::Manual::Glossary/Schema> object represents the database.
- use MyDatabase::Main qw();
- my $schema = MyDatabase::Main->connect('dbi:SQLite:db/example.db');
+ use MyApp::Schema qw();
+ my $schema = MyApp::Schema->connect('dbi:SQLite:db/example.db');
The first four arguments are the same as for L<DBI/connect>.
}
);
-Iterate over result objects of class C<MyDatabase::Main::Result::Artist>.
+Iterate over result objects of class C<MyApp::Schema::Result::Artist>.
L<Result|DBIx::Class::Manual::Glossary/Result> objects represent a row and
automatically get accessors for their column names.
DBIx-Class/examples/Schema$ perl ./insertdb.pl
# delete the schema classes files
- DBIx-Class/examples/Schema$ rm -rf MyDatabase/
+ DBIx-Class/examples/Schema$ rm -rf MyApp
# recreate schema classes files from database file
DBIx-Class/examples/Schema$ dbicdump \
- -o dump_directory=. MyDatabase::Main dbi:SQLite:db/example.db
+ -o dump_directory=. MyApp::Schema dbi:SQLite:db/example.db
=head2 Where to go next
=back
+ package MyApp::Schema;
__PACKAGE__->load_namespaces();
__PACKAGE__->load_namespaces(
result_namespace => 'Res',
resultset_namespace => 'RSet',
- default_resultset_class => '+MyDB::Othernamespace::RSet',
+ default_resultset_class => '+MyApp::Othernamespace::RSet',
);
With no arguments, this method uses L<Module::Find> to load all of the
Storage::DBI autodetects the underlying MySQL database, and re-blesses the
C<$storage> object into this class.
- my $schema = MyDb::Schema->connect( $dsn, $user, $pass, { on_connect_call => 'set_strict_mode' } );
+ my $schema = MyApp::Schema->connect( $dsn, $user, $pass, { on_connect_call => 'set_strict_mode' } );
=head1 DESCRIPTION