From: Arthur Axel 'fREW' Schmidt Date: Sat, 1 May 2010 08:46:30 +0000 (-0500) Subject: add authors, notes, and change LPW to Foo for root namespace X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=0cffcad79d18b3a4e241aaa29ef66e7e0d272831;p=dbsrgits%2Fdbix-class-introduction-presentation.git add authors, notes, and change LPW to Foo for root namespace --- diff --git a/slideshow.html b/slideshow.html index 980fe90..c058356 100644 --- a/slideshow.html +++ b/slideshow.html @@ -71,15 +71,37 @@

DBIX::Class (aka DBIC)

for (advanced) beginners

-

Leo Lapworth @ LPW 2009

-

http://leo.cuckoo.org/projects

-
+
+ +
+

Authors

+

Originally Leo Lapworth @ LPW 2009

+

Matthew S. Trout

+

Arthur Axel "fREW" Schmidt

+

Justin D. Hunter

assumptions

You know a little about Perl and using objects

You know a little bit about databases and using foreign keys

+
+
    +
  • How many people have designed a database with Foreign Keys?
  • +
  • How many people have used any ORM?
      +
    • In Perl?
        +
      • DBIC?
      • +
      • Class::DBI?
      • +
      • Rose::DB?
      • +
      • Fey?
      • +
      • Others?
      • +
    • +
    • AR?
    • +
    • DataMapper?
    • +
    • (N)Hibernate?
    • +
  • +
+
@@ -381,8 +403,8 @@ my @books = $book_model->search({

splitting logic cleanly

-

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

-

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

+

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

+

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

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

Schema::Loader

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

Schema::Loader

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

Schema::Loader

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

overloading

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

Result::

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

Result:: (inflating)

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

Result:: (inflating)

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

ResultSets::

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

ResultSets::

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

ResultSets::

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

ResultSets::

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

ResultSets::chaining

-
use LPW::DBIC;
-my $book_model   = LPW::DBIC->resultset('Books');
-my $author_model = LPW::DBIC->resultset('Authors');
+
use Foo::DBIC;
+my $book_model   = Foo::DBIC->resultset('Books');
+my $author_model = Foo::DBIC->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;
@@ -647,7 +669,7 @@ my @books = $rs->all;

overloading before new record

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

has_many

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

has_many

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

belongs_to

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

many_to_many

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

many_to_many

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

many_to_many

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

using many_to_many

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

error messages

-
DBIx::Class::Schema::Loader::connection(): Failed to load external class definition for 'LPW::DBIC::Result::Authors': Can't locate object method "many_to_many" via package "LPW::DBIC::Result::Author" at lib/LPW/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::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.

error messages

-
DBIx::Class::Schema::Loader::connection(): Failed to load external class definition for 'LPW::DBIC::Result::Authors': Can't locate object method "many_to_many" via package "LPW::DBIC::Result::Author" at lib/LPW/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::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.
@@ -888,14 +910,14 @@ $book->add_to_authors($author_2);

Catalyst

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