From: Arthur Axel 'fREW' Schmidt Date: Fri, 28 May 2010 05:10:29 +0000 (-0500) Subject: model != rs, Foo > MyApp X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=dbsrgits%2Fdbix-class-introduction-presentation.git;a=commitdiff_plain;h=451b57c2ed0502c69e02890b4db51a147c851934 model != rs, Foo > MyApp --- diff --git a/slideshow.html b/slideshow.html index 3b7f5db..47f8132 100644 --- a/slideshow.html +++ b/slideshow.html @@ -342,7 +342,7 @@ my @books = $book_model->search({
-
package MyApp::Schema::Result::Author;
+
package Foo::Schema::Result::Author;
 use strict; use warnings;
 __PACKAGE__->table('authors');
 __PACKAGE__->add_columns(
@@ -358,14 +358,14 @@ __PACKAGE__->add_columns(
 );
 __PACKAGE__->set_primary_key('id');
 __PACKAGE__->has_many( books =>
-   'MyApp::Schema::Result::Book', 'author_id'
+   'Foo::Schema::Result::Book', 'author_id'
 );
 1;
 
-
package MyApp::Schema::Result::Book;
+
package Foo::Schema::Result::Book;
 use strict; use warnings;
 __PACKAGE__->table('books');
 __PACKAGE__->add_columns(
@@ -386,7 +386,7 @@ __PACKAGE__->add_columns(
 );
 __PACKAGE__->set_primary_key('id');
 __PACKAGE__->belongs_to( author =>
-   'MyApp::Schema::Result::Author', 'author_id'
+   'Foo::Schema::Result::Author', 'author_id'
 );
 1;
 
@@ -395,7 +395,7 @@ __PACKAGE__->belongs_to( author =>

Schema::Loader

DB -> Perl vs Perl -> DB

-
package MyApp::Schema;
+
package Foo::Schema;
 use strict; use warnings;
 use base 'DBIx::Class::Schema::Loader';
 __PACKAGE__->loader_options({
@@ -406,14 +406,14 @@ __PACKAGE__->loader_options({
 
 # elsewhere...
 
-my $schema = MyApp::Schema->connect($dsn, $user, $pass);
+my $schema = Foo::Schema->connect($dsn, $user, $pass);
 

Splitting Logic Cleanly

-

MyApp::Schema::Result::Foo = individual row

-

MyApp::Schema::ResultSet::Foo = searches / table

+

Foo::Schema::Result::Bar = individual row

+

Foo::Schema::ResultSet::Bar = searches / table

@@ -421,8 +421,8 @@ my $schema = MyApp::Schema->connect($dsn, $user, $pass);
#!perl
 use strict; use warnings;
 use lib 'lib';
-use MyApp::Schema;
-my $schema = MyApp::Schema->connect($dns, $user, $pass);
+use Foo::Schema;
+my $schema = Foo::Schema->connect($dns, $user, $pass);
 my $author_rs = $schema->resultset('Author');
 my $author    = $author_rs->create({
    name => 'Douglas Adams',
@@ -449,10 +449,10 @@ INSERT INTO books (author, title)
 
    

overloading

-
MyApp::Schema::Result::Book
-MyApp::Schema::ResultSet::Book
-MyApp::Schema::Result::Author
-MyApp::Schema::ResultSet::Book
+
Foo::Schema::Result::Book
+Foo::Schema::ResultSet::Book
+Foo::Schema::Result::Author
+Foo::Schema::ResultSet::Book
@@ -490,34 +490,17 @@ use base 'DBIx::Class'; use strict; use warnings; +# Result code here + +__PACKAGE__->load_components('InflateColumn'); use DateTime::Format::MySQL; __PACKAGE__->inflate_column( date_published => { inflate => sub { DateTime::Format::MySQL->parse_date(shift) }, deflate => sub { shift->ymd}, - } -); -# Automatic see: DBIx::Class::InflateColumn::DateTime
-
- -
-

Result:: (inflating)

-
package Foo::Schema::Result::Books;
-use base 'DBIx::Class';
-use strict;
-use warnings;
-
-use DateTime::Format::MySQL;
-
-__PACKAGE__->inflate_column(
-   date_published => {
-      inflate => sub { DateTime::Format::MySQL->parse_date(shift) },
-      deflate => sub { shift->ymd},
-   }
+   },
 );
-# Automatic see: DBIx::Class::InflateColumn::DateTime
-# Automatic see: DBIx::Class::InflateColumn::DateTime
 # Automatic see: DBIx::Class::InflateColumn::DateTime
@@ -553,62 +536,31 @@ sub by_author {

ResultSets::

-
package Foo::Schema::ResultSet::Books;
-use base 'DBIx::Class::ResultSet';
-sub the_ultimate_books {
-   my $self = shift;
-   return $self->search({ title => { -like => '%42%' } })
-}
-sub by_author {
-   my ( $self, $author ) = @_;
-   return $self->search({ author => $author->id })
-}
-
-1;
-
- -
-

ResultSets::

-
package Foo::Schema::ResultSet::Books;
-use base 'DBIx::Class::ResultSet';
-sub the_ultimate_books {
-   my $self = shift;
-   return $self->search({ title => { -like => '%42%' } });
-}
-sub by_author {
-   my ( $self, $author ) = @_;
-   return $self->search({ author => $author->id })
-}
-
-1;
-
- -
-

ResultSets::

use Foo::Schema;
-my $book_model = Foo::Schema->resultset('Books');
-my $book_rs    = $book_model->the_ultimate_books;
-my @books      = $book_rs->all;
+my $schema = Foo::Schema->connect(...); +my $book_rs = Foo::Schema->resultset('Book'); +my $book_search = $book_rs->the_ultimate_books; +my @books = $book_search->all;
-

ResultSets::chaining

-
use Foo::Schema;
-my $book_model   = Foo::Schema->resultset('Books');
-my $author_model = Foo::Schema->resultset('Authors');
+      

ResultSets: Chaining

+
+my $book_rs      = $schema->resultset('Book');
+my $author_rs    = $schema->resultset('Author');
 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;
-

ResultSets::chaining

-
my $book_rs = $book_model
+      

ResultSets: Chaining

+
$book_rs = $schema->resultset('Book')
   ->the_ultimate_books
   ->by_author($author);
or -
my $book_rs = $book_model
+
my $book_rs = $schema->resultset('Book')
   ->the_ultimate_books();
 $book_rs = $book_rs->by_author($author);
# Debug (SQL):