Created
October 16, 2008 00:43
-
-
Save typester/17038 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## MySQL DB Schema | |
CREATE TABLE user ( | |
id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, | |
username VARCHAR(255) NOT NULL | |
); | |
CREATE TABLE bookmark ( | |
id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, | |
user INTEGER UNSIGNED NOT NULL, -- user.id | |
url VARCHAR(255) NOT NULL | |
); | |
## perl code sample | |
use strict; | |
use warnings; | |
{ | |
package DB; | |
use base qw/DBIx::Class::Schema::Loader/; | |
__PACKAGE__->loader_options; | |
} | |
my $schema = DB->connect("dbi:mysql:belongs_to", 'root'); | |
{ | |
# setup relations | |
package DB::User; | |
__PACKAGE__->has_many( bookmarks => 'DB::Bookmark', 'user' ); | |
package DB::Bookmark; | |
__PACKAGE__->belongs_to( user => 'DB::User' ); | |
} | |
# create users | |
my $typester = $schema->resultset('User')->create({ username => 'typester' }); | |
my $hidek = $schema->resultset('User')->create({ username => 'hidek' }); | |
# delete me | |
$typester->delete; | |
# add bookmark to deleted user | |
my $bookmark = $typester->add_to_bookmarks({ | |
url => 'http://www.google.com/', | |
}); | |
# call belongs_to method | |
my $user = $bookmark->user; | |
# check database! ex: "select * from user" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment