Hi Bliako,
I build my schemas the other way, from the database which I have created via an SQL file. I find the DBIx::Class code too gnarly to write by hand!
I used the following schema:
use Bliako; drop table if exists comment; drop table if exists event; drop table if exists user; create table user ( user_id smallint unsigned not null primary key auto_increment, name varchar(32) not null ); create table event ( event_id smallint unsigned not null primary key auto_increment, type enum('play','concert') not null, name varchar(32) not null, unique key type_name_uk (type, name) ); create table comment ( comment_id smallint unsigned not null primary key auto_increment, user_id smallint unsigned not null, event_id smallint unsigned not null, text blob, key event_id_key (event_id), key user_id_key (user_id), unique key event_user_uk (event_id, user_id), constraint event_id foreign key(event_id) references event (event_ +id), constraint user_id foreign key(user_id) references user (user_id) ); insert into user values (null, 'Bliako'), (null, '1nickt'); select user_id into @bliako_id from user where name = 'Bliako'; select user_id into @1nickt_id from user where name = '1nickt'; insert into event values (null, 'play', 'Hamlet'); insert into event values (null, 'concert', 'Rolling Stones'); select event_id into @play_id from event where name ='Hamlet'; select event_id into @concert_id from event where name ='Rolling Stone +s'; insert into comment values (null, @bliako_id, @play_id, 'Somewhat depressing'), (null, @bliako_id, @concert_id, 'Greatest rock and roll band in the +world!'), (null, @1nickt_id, @concert_id, 'There is cool, then there is Charli +e Watts.');
Then I created the DBIx schema with:
$ dbicdump -o dump_directory=. Bliako::Schema 'dbi:mysql:database=Blia +ko'
My test script:
use strict; use warnings; use feature 'say'; use Bliako::Schema; my $db = Bliako::Schema->connect( 'DBI:mysql:database=Bliako', undef, undef, { RaiseError => 1 }, ); for my $username ('Bliako', '1nickt') { my $user = $db->resultset('User')->search({ name => $username })-> +first; for my $comment ( $user->comments->all ) { say sprintf( '%s said about the %s "%s": "%s"', $username, $comment->event->type, $comment->event->name, $comment->text ); } }
Output:
perl -I. bliako.pl Bliako said about the play "Hamlet": "Somewhat depressing" Bliako said about the concert "Rolling Stones": "Greatest rock and rol +l band in the world!" 1nickt said about the concert "Rolling Stones": "There is cool, then t +here is Charlie Watts."
Hope this helps!
In reply to Re: DB: what kind of relationship?
by 1nickt
in thread DB: what kind of relationship?
by bliako
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |