Dbictalk 1227996894505619 9
Dbictalk 1227996894505619 9
Dbictalk 1227996894505619 9
http://leo.cuckoo.org/projects/
assumptions
Books
Authors
authors table
name varchar(255)
name varchar(255)
title varchar(255),
author int(8),
references authors(id)
$sth->execute(
'A book title',$author_id
);
manual: create
my $sth = $dbh->prepare('
INSERT INTO books
(title, author)
values (?,?)
');
$sth->execute(
'A book title', $author_id
);
manual: retrieve
my $sth = $dbh->prepare('
SELECT title,
authors.name as author_name
FROM books, authors
WHERE books.author = authors.id
');
manual: retrieve
while( my $book = $sth->fetchrow_hashref() )
{
print 'Author of '
. $book->{title}
. ' is '
. $book->{author_name}
. "\n";
}
manual: update
my $update = $dbh->prepare('
UPDATE books
SET title = ?
WHERE id = ?
');
$update->execute(
'New title', $book_id);
manual: delete
my $delete = $dbh->prepare('
DELETE FROM books
WHERE id = ?
');
$delete->execute( $book_id);
DBIx::Class
DBIC: create
my $book = $book_model->create({
title => 'A book title',
author => $author_id,
});
my $book = $book_model->search({
title => 'A book title',
})->single();
my @books = $book_model->search({
author => $author_id,
})->all();
DBIC: retrieve
while( my $book = $books_rs->next() ) {
print 'Author of '
. $book->title()
. ' is '
. $book->author()->name()
. "\n";
}
DBIC: retrieve
my $books_rs = $book_model->search({
author => $author_id,
});
too much
maintenance!
Schema::Loader
Tip
LPW::DBIC::Result::XX
LPW::DBIC::ResultSet::XX
splitting logic
cleanly
LPW::DBIC::Result::Books->belongs_to("author",
"LPW::DBIC::Authors", { id => "author" });
Schema::Loader
SQL - debugging
LPW::DBIC::Result::Books
LPW:: DBIC::ResultSet::Books
LPW:: DBIC::Result::Authors
LPW:: DBIC::ResultSet::Authors
Result::
package LPW::DBIC::Result::Books;
use base 'DBIx::Class';
use strict;
use warnings;
sub isbn {
my $self = shift;
return $api->isbn();
}
1;
Result::
package LPW::DBIC::Result::Books;
use base 'DBIx::Class';
use strict;
use warnings;
sub isbn {
my $self = shift;
return $api->isbn();
}
1;
Result::
print $book->isbn();
Result:: (inflating)
package LPW::DBIC::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
Result:: (inflating)
package LPW::DBIC::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
Result:: (deflating)
$book->date_published(DateTime->now);
$book->update();
2008-11-29
Result:: (inflating)
my $date_published = $book->date_published()
print $date_published->month_abbr();
Nov
ResultSets::
package LPW::DBIC::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 ) = @_;
1;
ResultSets::
package LPW::DBIC::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 ) = @_;
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(),
} );
}
ResultSets::
use LPW::DBIC;
my $book_model = LPW::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');
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
->the_ultimate_books()
->by_author($author);
or
my $book_rs = $book_model
->the_ultimate_books();
$book_rs = $book_rs->by_author($author);
# Debug (SQL):
my @books = $rs->all();
overloading before
new record
overloading before
new record
package LPW::DBIC::Result::Authors;
use base 'DBIx::Class';
sub new {
my ( $class, $attrs ) = @_;
my $new = $class->next::method($attrs);
return $new;
}
1;
relationships
multiple authors
a few relationships
has_many has_many
belongs_to belongs_to
many_to_many
a few relationships
!
new join table
CREATE TABLE author_and_books(
id int(8) primary key auto_increment,
book
int(8),
author int(8),
Books Authors_and_Books
belongs_to
has_many
package LPW::DBIC::Result::Books;
__PACKAGE__->has_many(
"author_and_books",
"LPW::DBIC::Result::AuthorAndBooks",
);
__PACKAGE__->has_many(
"author_and_books",
# Name of accessor
"LPW::DBIC::Result::AuthorAndBooks",
# Related class
{ "foreign.book" => "self.id" },
# Relationship (magic often works if not
# specified, but avoid!)
);
belongs_to
has_many
Books Authors_and_Books
belongs_to
belongs_to
package LPW::DBIC::Result::AuthorAndBooks;
__PACKAGE__->belongs_to(
"book",
"LPW::DBIC::Result::Books",
{ id => "book" }
);
__PACKAGE__->belongs_to(
"book", # Accessor name
"LPW::DBIC::Result::Books", # Related class
{ id => "book" } # Relationship
);
same for Authors
has_many
Authors Authors_and_Books
belongs_to
with no coding...
has_many has_many
belongs_to belongs_to
many_to_many
has_many has_many
belongs_to belongs_to
many_to_many
many_to_many
package LPW::DBIC::Result::Books;
use base 'DBIx::Class';
__PACKAGE__->many_to_many(
"authors"
=> "author_and_books",
'author'
);
1;
__PACKAGE__->many_to_many(
"authors"
# Accessor Name
=> "author_and_books",
# has_many accessor_name
'author'
# foreign relationship name
);
1;
many_to_many
package LPW::DBIC::Result::Authors;
use base 'DBIx::Class';
__PACKAGE__->many_to_many(
"books"
# Accessor Name
=> "author_and_books",
# has_many accessor_name
'book'
# foreign relationship name
);
1;
use LPW::DBIC;
my $author_model = LPW::DBIC->resultset('Authors');
my $author = $author_model->search({
name => 'Douglas Adams',
})->single();
$author->add_to_books({
title => 'A new book',
});
using many_to_many
my $author = $author_model->search({
name => 'Douglas Adams',
})->single();
$author->add_to_books({
title => 'A new book',
});
$book->add_to_authors($author_1);
$book->add_to_authors($author_2);
in 16 lines of code
has_many has_many
belongs_to belongs_to
many_to_many
errors
• Turn on debugging
• Read error messages (sometimes useful!)
• Check field names
• Check package names
• Check which database you are connected
to (development/test/live?) - repeat above
thanks
http://leo.cuckoo.org/projects/
use strict;
use warnings;
__PACKAGE__->config(
schema_class => 'LPW::DBIC',
);
1;
Catalyst
package Your::App::Model::LPW;
use base qw(Catalyst::Model::DBIC::Schema);
use strict;
use warnings;
__PACKAGE__->config(
schema_class => 'LPW::DBIC',
);
1;
my $model = $c->model('DBIC::LPW');
my $author_model = $model->resultset('Authors');
1;
thanks!
http://leo.cuckoo.org/projects/