From: Arthur Axel 'fREW' Schmidt Date: Sat, 1 May 2010 08:51:45 +0000 (-0500) Subject: use the more idiomatic Schema instead of DBIC X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=41a0eb8ea0676647d513a9b7934f68c5f1b3fa3f;p=dbsrgits%2Fdbix-class-introduction-presentation.git use the more idiomatic Schema instead of DBIC --- diff --git a/slideshow.html b/slideshow.html index c058356..68f2a07 100644 --- a/slideshow.html +++ b/slideshow.html @@ -403,8 +403,8 @@ my @books = $book_model->search({

splitting logic cleanly

-

Foo::DBIC::Result::Foo = an individual row

-

Foo::DBIC::ResultSet::Foo = searches / results

+

Foo::Schema::Result::Foo = an individual row

+

Foo::Schema::ResultSet::Foo = searches / results

@@ -419,8 +419,8 @@ my @books = $book_model->search({

Schema::Loader

-
Foo::DBIC::Result::Authors->table("authors");
-Foo::DBIC::Result::Authors->add_columns(
+
Foo::Schema::Result::Authors->table("authors");
+Foo::Schema::Result::Authors->add_columns(
    id => {
       data_type     => "INT",
       default_value => undef,
@@ -434,13 +434,13 @@ Foo::DBIC::Result::Authors->add_columns(
       size          => 255,
    },
 );
-Foo::DBIC::Result::Authors->set_primary_key("id");
+Foo::Schema::Result::Authors->set_primary_key("id");

Schema::Loader

-
Foo::DBIC::Result::Books->table("books");
-Foo::DBIC::Result::Books->add_columns(
+
Foo::Schema::Result::Books->table("books");
+Foo::Schema::Result::Books->add_columns(
    id => {
       data_type     => "INT",
       default_value => undef,
@@ -460,15 +460,15 @@ Foo::DBIC::Result::Books->add_columns(
       size          => 8
    },
 );
-Foo::DBIC::Result::Books->set_primary_key("id");
+Foo::Schema::Result::Books->set_primary_key("id");

Schema::Loader

-
Foo::DBIC::Result::Authors->has_many(books => "Foo::DBIC::Books",
+
Foo::Schema::Result::Authors->has_many(books => "Foo::Schema::Books",
    { "foreign.author" => "self.id" });
 
-Foo::DBIC::Result::Books->belongs_to(author => "Foo::DBIC::Authors",
+Foo::Schema::Result::Books->belongs_to(author => "Foo::Schema::Authors",
    { id => "author" });
@@ -483,15 +483,15 @@ INSERT INTO books (author, title)

overloading

-
Foo::DBIC::Result::Books
-Foo::DBIC::ResultSet::Books
-Foo::DBIC::Result::Authors
-Foo::DBIC::ResultSet::Books
+
Foo::Schema::Result::Books
+Foo::Schema::ResultSet::Books
+Foo::Schema::Result::Authors
+Foo::Schema::ResultSet::Books

Result::

-
package Foo::DBIC::Result::Books;
+
package Foo::Schema::Result::Books;
 use base 'DBIx::Class';
 use strict;
 use warnings;
@@ -517,7 +517,7 @@ sub isbn {
 
    

Result:: (inflating)

-
package Foo::DBIC::Result::Books;
+
package Foo::Schema::Result::Books;
 use base 'DBIx::Class';
 use strict;
 use warnings;
@@ -535,7 +535,7 @@ __PACKAGE__->inflate_column(
 
    

Result:: (inflating)

-
package Foo::DBIC::Result::Books;
+
package Foo::Schema::Result::Books;
 use base 'DBIx::Class';
 use strict;
 use warnings;
@@ -569,7 +569,7 @@ print $date_published->month_abbr;

ResultSets::

-
package Foo::DBIC::ResultSet::Books;
+
package Foo::Schema::ResultSet::Books;
 use base 'DBIx::Class::ResultSet';
 sub the_ultimate_books {
    my $self = shift;
@@ -585,7 +585,7 @@ sub by_author {
 
    

ResultSets::

-
package Foo::DBIC::ResultSet::Books;
+
package Foo::Schema::ResultSet::Books;
 use base 'DBIx::Class::ResultSet';
 sub the_ultimate_books {
    my $self = shift;
@@ -601,7 +601,7 @@ sub by_author {
 
    

ResultSets::

-
package Foo::DBIC::ResultSet::Books;
+
package Foo::Schema::ResultSet::Books;
 use base 'DBIx::Class::ResultSet';
 sub the_ultimate_books {
    my $self = shift;
@@ -617,17 +617,17 @@ sub by_author {
 
    

ResultSets::

-
use Foo::DBIC;
-my $book_model = Foo::DBIC->resultset('Books');
+
use Foo::Schema;
+my $book_model = Foo::Schema->resultset('Books');
 my $book_rs    = $book_model->the_ultimate_books;
 my @books      = $book_rs->all;

ResultSets::chaining

-
use Foo::DBIC;
-my $book_model   = Foo::DBIC->resultset('Books');
-my $author_model = Foo::DBIC->resultset('Authors');
+
use Foo::Schema;
+my $book_model   = Foo::Schema->resultset('Books');
+my $author_model = Foo::Schema->resultset('Authors');
 my $author       = $author_model->search({ name => 'Douglas Adams' })->single;
 my $book_rs      = $book_model->the_ultimate_books->by_author($author);
 my @books        = $book_rs->all;
@@ -669,7 +669,7 @@ my @books = $rs->all;

overloading before new record

-
package Foo::DBIC::Result::Authors;
+      
package Foo::Schema::Result::Authors;
 use base 'DBIx::Class';
 
 sub new {
@@ -731,9 +731,9 @@ ALTER TABLE `books` DROP `author`

has_many

-
package Foo::DBIC::Result::Books;
+
package Foo::Schema::Result::Books;
 
-__PACKAGE__->has_many( author_and_books => "Foo::DBIC::Result::AuthorAndBooks",
+__PACKAGE__->has_many( author_and_books => "Foo::Schema::Result::AuthorAndBooks",
     { "foreign.book" => "self.id" },
 );
 
@@ -742,11 +742,11 @@ __PACKAGE__->has_many( author_and_books => "Foo::DBIC::Result::AuthorAndBo
 
    

has_many

-
package Foo::DBIC::Result::Books;
+
package Foo::Schema::Result::Books;
 
 __PACKAGE__->has_many(
 author_and_books => # name of accessor
-"Foo::DBIC::Result::AuthorAndBooks", # related class
+"Foo::Schema::Result::AuthorAndBooks", # related class
     { "foreign.book" => "self.id" } # Relationship (magic often works if not
                                         # specified, but avoid!)
 );
@@ -759,11 +759,11 @@ author_and_books => # name of accessor
 
    

belongs_to

-
package Foo::DBIC::Result::AuthorAndBooks;
+
package Foo::Schema::Result::AuthorAndBooks;
 
 __PACKAGE__->belongs_to(
    book => # Accessor name
-   "Foo::DBIC::Result::Books", # Related class
+   "Foo::Schema::Result::Books", # Related class
    { id => "book" } # relationship
 );
 
@@ -783,7 +783,7 @@ __PACKAGE__->belongs_to(

many_to_many

-
package Foo::DBIC::Result::Books;
+      
package Foo::Schema::Result::Books;
 use base 'DBIx::Class';
 
 __PACKAGE__->many_to_many(
@@ -797,7 +797,7 @@ __PACKAGE__->many_to_many(
 
    

many_to_many

-
package Foo::DBIC::Result::Books;
+      
package Foo::Schema::Result::Books;
 use base 'DBIx::Class';
 
 __PACKAGE__->many_to_many(
@@ -811,7 +811,7 @@ __PACKAGE__->many_to_many(
 
    

many_to_many

-
package Foo::DBIC::Result::Authors;
+      
package Foo::Schema::Result::Authors;
 use base 'DBIx::Class';
 
 __PACKAGE__->many_to_many(
@@ -829,9 +829,9 @@ __PACKAGE__->many_to_many(
       

using many_to_many

#!/usr/bin/perl
 
-use Foo::DBIC;
+use Foo::Schema;
 
-my $author_model = Foo::DBIC->resultset('Authors');
+my $author_model = Foo::Schema->resultset('Authors');
 my $author       = $author_model->search({
    name => 'Douglas Adams',
 })->single;
@@ -875,12 +875,12 @@ $book->add_to_authors($author_2);

error messages

-
DBIx::Class::Schema::Loader::connection(): Failed to load external class definition for 'Foo::DBIC::Result::Authors': Can't locate object method "many_to_many" via package "Foo::DBIC::Result::Author" at lib/Foo/DBIC/Result/Authors.pm line 9.Compilation failed in require at /Library/Perl/5.8.8/DBIx/Class/Schema/Loader/Base.pm line 292.
+
DBIx::Class::Schema::Loader::connection(): Failed to load external class definition for 'Foo::Schema::Result::Authors': Can't locate object method "many_to_many" via package "Foo::Schema::Result::Author" at lib/Foo/Schema/Result/Authors.pm line 9.Compilation failed in require at /Library/Perl/5.8.8/DBIx/Class/Schema/Loader/Base.pm line 292.

error messages

-
DBIx::Class::Schema::Loader::connection(): Failed to load external class definition for 'Foo::DBIC::Result::Authors': Can't locate object method "many_to_many" via package "Foo::DBIC::Result::Author" at lib/Foo/DBIC/Result/Authors.pm line 9.Compilation failed in require at /Library/Perl/5.8.8/DBIx/Class/Schema/Loader/Base.pm line 292.
+
DBIx::Class::Schema::Loader::connection(): Failed to load external class definition for 'Foo::Schema::Result::Authors': Can't locate object method "many_to_many" via package "Foo::Schema::Result::Author" at lib/Foo/Schema/Result/Authors.pm line 9.Compilation failed in require at /Library/Perl/5.8.8/DBIx/Class/Schema/Loader/Base.pm line 292.
@@ -911,13 +911,13 @@ $book->add_to_authors($author_2);

Catalyst

package Your::App::Model::Foo;
-use base qw(Catalyst::Model::DBIC::Schema);
+use base qw(Catalyst::Model::Schema::Schema);
 
 use strict;
 use warnings;
 
 __PACKAGE__->config(
-  schema_class => 'Foo::DBIC',
+  schema_class => 'Foo::Schema',
 );
 
 1;
@@ -929,7 +929,7 @@ __PACKAGE__->config(
sub action_name : Local {
   my ($self, $c) = @_;
 
-  my $model = $c->model('DBIC::Foo');
+  my $model = $c->model('Schema::Foo');
   my $author_model = $model->resultset('Authors');
 
 }