</div>
<div class="slide">
- <h1>books table</h1>
-<pre>CREATE TABLE books(
- id int(8) primary key auto_increment,
- title varchar(255),
- author_id <strong>int(8)</strong>,<strong>foreign key (<em>author</em>)</strong>
- <strong>references <em>authors(id)</em></strong>
-) engine = InnoDB DEFAULT CHARSET=utf8;</pre>
- </div>
-
- <div class="slide">
<h1>CRUD compared</h1>
<ul>
<li><strong>C</strong> - Create</li>
<h1>SQL: Create</h1>
<pre>my $sth = $dbh->prepare('
INSERT INTO books
- (title, author)
+ (title, author_id)
values (?,?)
');
<div class="slide">
<h1>DBIC: Create</h1>
-<pre>my $book = $book_model->create({
- title => 'A book title',
- author => $author_id,
+<pre>my $book = $book_rs->create({
+ title => 'A book title',
+ author_id => $author_id,
});</pre>
<p>Look ma, no SQL!</p>
</div>
<div class="slide">
<h1>DBIC: Create</h1>
-<pre>my $pratchett = $author_model->create({
+<pre>my $pratchett = $author_rs->create({
name => 'Terry Pratchett',
});</pre>
</div>
<div class="slide">
<h1>DBIC: Read</h1>
- <p>DBIx::Class - Lots of ways to do the same thing...</p>
- <p><em>"There is more than one way to do it (TIMTOWTDI, usually pronounced "Tim Toady") is a Perl motto"</em></p>
+ <p>DBIx::Class - TIMTOWTDI</p>
</div>
<div class="slide">
<h1>DBIC: Read</h1>
-<pre>my $book = $book_model->find($book_id);
+<pre>my $book = $book_rs->find($book_id);
-my $book = $book_model->search({
+my $book = $book_rs->search({
title => 'A book title',
}, { rows => 1 })->single;
-my @books = $book_model->search({
+my @books = $book_rs->search({
author => $author_id,
})->all;</pre>
</div>
<div class="slide">
<h1>DBIC: Read</h1>
-<pre>my $books_rs = $book_rs->search({
+<pre>my $resultset = $book_rs->search({
author => $author_id,
});</pre>
<p>Search takes SQL::Abstract formatted queries</p>
<div class="slide">
<pre>package Foo::Schema::Result::Author;
-use strict; use warnings;
__PACKAGE__->table('authors');
__PACKAGE__->add_columns(
id => {
- data_type => 'int',
- size => 8,
+ data_type => 'int',
+ is_auto_increment => 1
},
title => {
data_type => 'varchar',
</div>
<div class="slide">
-<pre>package Foo::Schema::Result::Book;
+<pre style="float: left; font-size: 80%;">package Foo::Schema::Result::Book;
use strict; use warnings;
__PACKAGE__->table('books');
__PACKAGE__->add_columns(
id => {
- data_type => 'int',
- size => 8,
+ data_type => 'int',
+ is_auto_increment => 1
},
name => {
data_type => 'varchar',
is_nullable => 1,
size => 255,
},
+</pre>
+<pre style="float: left; font-size: 80%;">
author_id => {
data_type => 'int',
size => 8,
- is_nullable => 1, # <-- probably should be 0
},
);
__PACKAGE__->set_primary_key('id');
<div class="slide">
<h1>ResultSets: Chaining</h1>
<pre>
-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;</pre>
+my $book_rs = $schema->resultset('Book');
+my $author_rs = $schema->resultset('Author');
+my $author = $author_rs->search({ name => 'Douglas Adams' })->single;
+$book_rs = $book_rs->the_ultimate_books->by_author($author);
+my @books = $book_rs->all;</pre>
</div>
<div class="slide">
</div>
<div class="slide">
- <h1>ResultSets::chaining</h1>
-<pre>my $rs = $book_model
+ <h1>ResultSets: Chaining</h1>
+<pre>my $rs = $book_rs
->category('childrens')
->by_author($author)
->published_after('1812')
<div class="slide">
<h1>Multiple Authors</h1>
+ <p>We want to allow a book to be by more than one author</p>
</div>
<div class="slide">
foreign key (author_id) references authors(id)
) engine = InnoDB DEFAULT CHARSET=utf8;
-ALTER TABLE `books` DROP `author`;</pre>
+ALTER TABLE `books` DROP `author_id`;</pre>
</div>
<div class="slide">
use Foo::Schema;
my $schema = Foo::Schema->connect(...);
my $author_rs = $schema->resultset('Authors');
-my $author = $author_model->search({
+my $author = $author_rs->search({
name => 'Douglas Adams',
})->single;
$author->add_to_books({
<div class="slide">
<h1>using many_to_many</h1>
- <pre>my $author = $author_model->search({
+ <pre>my $author = $author_rs->search({
name => 'Douglas Adams',
})->single;
<strong>$author->add_to_books({
<div class="slide">
<h1>error messages</h1>
- <pre>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.</pre>
+<pre>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.</pre>
</div>
<div class="slide">
<h1>error messages</h1>
- <pre>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::<strong>Result::Author</strong>" at lib/Foo/Schema/<strong>Result/Authors.pm</strong> line 9.Compilation failed in require at /Library/Perl/5.8.8/DBIx/Class/Schema/Loader/Base.pm line 292.</pre>
+<pre>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::<strong>Result::Author</strong>" at
+lib/Foo/Schema/<strong>Result/Authors.pm</strong> line 9.Compilation failed in require at
+/Library/Perl/5.8.8/DBIx/Class/Schema/Loader/Base.pm line 292.</pre>
</div>
<div class="slide">
<li>HashRefInflator</li>
<li>Subqueries</li>
<li>ResultSetColumn</li>
+ <li>Aggregate Queries</li>
</ul>
</div>
);
1;</pre>
- <p>Keep your Schema in a <em>separate</em> package to your Catalyst application</p>
+ <p>Keep your Schema in a <em>separate</em> package from your Catalyst application</p>
</div>
<div class="slide">
<pre>sub action_name : Local {
my ($self, $c) = @_;
- my $model = $c->model('Schema::Foo');
- my $author_model = $model->resultset('Authors');
+ my $schema = $c->model('Schema::Foo');
+ my $author_rs = $schema->resultset('Authors');
}