X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FModel%2FDBIC%2FSchema.pm;h=2406196abeab10931907efdf29b793332180aea1;hb=87145c6c724aa7bd530d7d80fd3c0830a72ec17e;hp=d9fc47dc2d6135b21da408597593532578f1cf8a;hpb=ca7cf6f02f7378154b24cc6a6d4c66dee50cf902;p=catagits%2FCatalyst-Model-DBIC-Schema.git diff --git a/lib/Catalyst/Model/DBIC/Schema.pm b/lib/Catalyst/Model/DBIC/Schema.pm index d9fc47d..2406196 100644 --- a/lib/Catalyst/Model/DBIC/Schema.pm +++ b/lib/Catalyst/Model/DBIC/Schema.pm @@ -1,11 +1,12 @@ package Catalyst::Model::DBIC::Schema; +use 5.008_001; use Moose; use mro 'c3'; extends 'Catalyst::Model'; with 'CatalystX::Component::Traits'; -our $VERSION = '0.30'; +our $VERSION = '0.33'; $VERSION = eval $VERSION; use namespace::autoclean; @@ -24,107 +25,65 @@ Catalyst::Model::DBIC::Schema - DBIx::Class::Schema Model Class =head1 SYNOPSIS -Manual creation of a DBIx::Class::Schema and a Catalyst::Model::DBIC::Schema: +First, prepare your database schema using L, see +L for how to generate a +L from your database using the Helper script, and +L. -=over +A typical usage of the helper script would be: -=item 1. + script/myapp_create.pl model FilmDB DBIC::Schema MyApp::Schema::FilmDB \ + create=static dbi:mysql:filmdb dbusername dbpass \ + quote_char='`' name_sep='.' -Create the DBIx:Class schema in MyApp/Schema/FilmDB.pm: +If you are unfamiliar with L, see L +first. - package MyApp::Schema::FilmDB; - use base qw/DBIx::Class::Schema/; +These examples assume that you already have a schema called +C, which defines some Result classes for tables in +C and +C. Either created by the helper script (as +shown above) or manually. - __PACKAGE__->load_classes(qw/Actor Role/); +The helper also creates a Model in C, if you already +have a schema you can create just the Model using: -=item 2. + script/myapp_create.pl model FilmDB DBIC::Schema MyApp::Schema::FilmDB + dbi:mysql:filmdb dbusername dbpass -Create some classes for the tables in the database, for example an -Actor in MyApp/Schema/FilmDB/Actor.pm: +The connect_info is optional and will be hardcoded into the Model if provided. +It's better to configure it in your L config file, which will also +override any hardcoded config, see L for examples. - package MyApp::Schema::FilmDB::Actor; - use base qw/DBIx::Class/ - - __PACKAGE__->load_components(qw/Core/); - __PACKAGE__->table('actor'); - - ... - -and 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'); - - ... - -Notice that the schema is in MyApp::Schema, not in MyApp::Model. This way it's -usable as a standalone module and you can test/run it without Catalyst. - -=item 3. - -To expose it to Catalyst as a model, you should create a DBIC Model in -MyApp/Model/FilmDB.pm: - - package MyApp::Model::FilmDB; - use base qw/Catalyst::Model::DBIC::Schema/; - - __PACKAGE__->config( - schema_class => 'MyApp::Schema::FilmDB', - connect_info => { - dsn => "DBI:...", - user => "username", - password => "password", - } - ); - -See below for a full list of the possible config parameters. +Now you have a working Model which accesses your separate DBIC Schema. This can +be used/accessed in the normal Catalyst manner, via C<< $c->model() >>: -=back + my $db_model = $c->model('FilmDB'); # a Catalyst::Model + my $dbic = $c->model('FilmDB')->schema; # the actual DBIC object -Now you have a working Model which accesses your separate DBIC Schema. This can -be used/accessed in the normal Catalyst manner, via $c->model(): +The Model proxies to the C instance so you can do: - my $actor = $c->model('FilmDB::Actor')->find(1); + my $rs = $db_model->resultset('Actor'); # ... or ... + my $rs = $dbic ->resultset('Actor'); # same! -You can also use it to set up DBIC authentication with -L in MyApp.pm: +There is also a shortcut, which returns a L directly, +instead of a L: - package MyApp; + my $rs = $c->model('FilmDB::Actor'); - use Catalyst qw/... Authentication .../; +See L to find out more about which methods can be +called on ResultSets. - ... +You can also define your own ResultSet methods to encapsulate the +database/business logic of your applications. These go into, for example, +C. The class must inherit from +L and is automatically loaded. - __PACKAGE__->config->{authentication} = - { - default_realm => 'members', - realms => { - members => { - credential => { - class => 'Password', - password_field => 'password', - password_type => 'hashed' - password_hash_type => 'SHA-256' - }, - store => { - class => 'DBIx::Class', - user_model => 'DB::User', - role_relation => 'roles', - role_field => 'rolename', - } - } - } - }; +Then call your methods like any other L method: -C<< $c->model('Schema::Source') >> returns a L for -the source name parameter passed. To find out more about which methods can -be called on a ResultSet, or how to add your own methods to it, please see -the ResultSet documentation in the L distribution. + $c->model('FilmDB::Actor')->SAG_members -Some examples are given below: +=head2 Some examples: # to access schema methods directly: $c->model('FilmDB')->schema->source(...); @@ -154,17 +113,19 @@ Some examples are given below: $newconn->storage_type('::LDAP'); $newconn->connection(...); +To set up authentication, see L below. + =head1 DESCRIPTION This is a Catalyst Model for L-based Models. See the documentation for L for information on generating these Models via Helper scripts. -When your Catalyst app starts up, a thin Model layer is created as an -interface to your DBIC Schema. It should be clearly noted that the model -object returned by C<< $c->model('FilmDB') >> is NOT itself a DBIC schema or -resultset object, but merely a wrapper proving L to access -the underlying schema. +When your Catalyst app starts up, a thin Model layer is created as an interface +to your DBIC Schema. It should be clearly noted that the model object returned +by C<< $c->model('FilmDB') >> is NOT itself a DBIC schema or resultset object, +but merely a wrapper proving L to access the underlying +schema (but also proxies other methods to the underlying schema.) In addition to this model class, a shortcut class is generated for each source in the schema, allowing easy and direct access to a resultset of the @@ -194,8 +155,15 @@ resultset object: In order to add methods to a DBIC resultset, you cannot simply add them to the source (row, table) definition class; you must define a separate custom -resultset class. See L -for more info. +resultset class. This is just a matter of making a +C class that inherits from +L, if you are using +L, the default for helper script generated +schemas. + +See L +for information on definining your own L classes for +use with L, the old default. =head1 CONFIG PARAMETERS @@ -495,13 +463,15 @@ sub BUILD { $self->composed_schema($schema_class->compose_namespace($class)); + my $was_mutable = $self->meta->is_mutable; + $self->meta->make_mutable; $self->meta->add_attribute('schema', is => 'rw', isa => 'DBIx::Class::Schema', handles => $self->_delegates ); - $self->meta->make_immutable; + $self->meta->make_immutable unless $was_mutable; $self->schema($self->composed_schema->clone); @@ -653,11 +623,42 @@ __PACKAGE__->meta->make_immutable; =item CMDS_NO_SOURCES -Set this variable if you will be using schemas with no sources (tables) to -disable the warning. The warning is there because this is usually a mistake. +Set this variable if you will be using schemas with no sources (Result classes) +to disable the warning. The warning is there because having no Result classes +is usually a mistake. =back +=head1 Setting up DBIC authentication + +You can set this up with +L in MyApp.pm: + + package MyApp; + + use Catalyst qw/... Authentication .../; + + ... + + __PACKAGE__->config('Plugin::Authentication' => + { + default_realm => 'members', + members => { + credential => { + class => 'Password', + password_field => 'password', + password_type => 'hashed' + password_hash_type => 'SHA-256' + }, + store => { + class => 'DBIx::Class', + user_model => 'DB::User', + role_relation => 'roles', + role_field => 'rolename', + } + } + }); + =head1 SEE ALSO General Catalyst Stuff: @@ -674,7 +675,8 @@ L, L Traits: L, -L +L, +L =head1 AUTHOR @@ -684,12 +686,26 @@ Brandon L Black C caelum: Rafael Kitover C -Dan Dascalescu C +dandv: Dan Dascalescu C + +bluefeet: Aran Deltac C -Aran Deltac C +t0m: Tomas Doran C + +osfameron: C + +ozum: Ozum Eldogan C + +Pavel I. Shaydo C =head1 COPYRIGHT +Copyright (c) 2006 - 2009 +the Catalyst::Model::DBIC::Schema L and L +as listed above. + +=head1 LICENSE + This program is free software. You can redistribute it and/or modify it under the same terms as Perl itself.