DBIx::Class::Schema Model Class is a software tool used for creating model classes that represent database tables, allowing for easy manipulation of data. It streamlines the process of querying databases and modifying table structures in object-oriented programming.
Furthermore, a shortcut class is created for each source in the schema for seamless and straightforward direct access to a corresponding type's resultset. These shortcut classes are even thinner than the model class, and they provide no public methods; instead, they merely hook into Catalyst's model() accessor through the ACCEPT_CONTEXT mechanism. The following is roughly equivalent to the entire contents of each generated class:
package MyApp::Model::FilmDB::Actor
sub ACCEPT_CONTEXT {
my ($self, $c) = @_;
$c->model('FilmDB')->resultset('Actor');
}
There are three techniques to obtain a DBIC resultset object:
- the long way: my $rs = $c->model('FilmDB')->schema->resultset('Actor');
- using the shortcut method on the model object: my $rs = $c->model('FilmDB')->resultset('Actor');
- using the generated class directly: my $rs = $c->model('FilmDB::Actor');
If you need to add methods to a DBIC resultset, you can't just do that by adding them to the source (row, table) definition class; instead, you need to define a custom resultset class. For more information, please see "Predefined searches" in DBIx::Class::Manual::Cookbook.
Here's how to manually create a DBIx::Class::Schema and a Catalyst::Model::DBIC::Schema:
1. Create the DBIx::Class schema at MyApp/Schema/FilmDB.pm:
package MyApp::Schema::FilmDB;
use base qw/DBIx::Class::Schema/;
__PACKAGE__->load_classes(qw/Actor Role/);
2. Create some classes for the tables in the database. For instance, create an Actor in MyApp/Schema/FilmDB/Actor.pm:
package MyApp::Schema::FilmDB::Actor;
use base qw/DBIx::Class/
__PACKAGE__->load_components(qw/Core/);
__PACKAGE__->table('actor');
...
And create a Role in MyApp/Schema/FilmDB/Role.pm:
package MyApp::Schema::FilmDB::Role;
use base qw/DBIx::Class/
__PACKAGE__->load_components(qw/Core/);
__PACKAGE__->table('role');
...
Note that the schema is in MyApp::Schema, not in MyApp::Model. This means that it's functional as a standalone module that can be tested and executed without Catalyst.
3. To expose it to Catalyst as a model, create a DBIC Model at MyApp/Model/FilmDB.pm:
package MyApp::Model::FilmDB;
use base qw/Catalyst::Model::
Version 0.29: N/A