Thread DBIx::Class -> has_many und belongs_to (8 answers)
Opened by bloonix at 2007-09-23 15:20

bloonix
 2007-09-23 15:20
#99880 #99880
User since
2005-12-17
1615 Artikel
HausmeisterIn
[Homepage]
user image
Hallo Community,

vielleicht kann mir jemand bei einem Verständnisproblem mit der
Funktion belongs_to() und foreign.key und self.key weiterhelfen.
In dem sehr einfachen Beispiel möchte ich gerne mit dem Aufruf
von belongs_to() in BloonixDB::UserGroup die Spaltennamen als
drittes Argument mitgeben.

Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
CREATE TABLE user (
id SERIAL PRIMRAY KEY,
username VARCHAR(200) UNIQUE,
password VARCHAR(200),
locked BOOLEAN DEFAULT 1,
email VARCHAR(200)
);

CREATE TABLE group (
id SERIAL PRIMARY KEY,
groupname VARCHAR(24) UNIQUE,
description VARCHAR(200)
);

CREATE TABLE user_group (
user_id INTEGER REFERENCES user(id),
group_id INTEGER REFERENCES group(id),
PRIMARY KEY(user_id, group_id)
);


Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package BloonixDB::User;

use base qw/DBIx::Class/;

__PACKAGE__->load_components(qw/PK::Auto Core/);
__PACKAGE__->table('user');
__PACKAGE__->add_columns(qw/id username password locked email/);
__PACKAGE__->set_primary_key(qw/id/);
__PACKAGE__->has_many('user_groups', 'BloonixDB::UserGroup', { 'foreign.user_id' => 'self.id' });

package BloonixDB::Group;

use base qw/DBIx::Class/;

__PACKAGE__->load_components(qw/PK::Auto Core/);
__PACKAGE__->table('group');
__PACKAGE__->add_columns(qw/id groupname description/);
__PACKAGE__->set_primary_key('id');
__PACKAGE__->has_many('user_groups', 'BloonixDB::UserGroup', { 'foreign.group_id' => 'self.id' });

package BloonixDB::UserGroup;

__PACKAGE__->load_components(qw/PK::Auto Core/);
__PACKAGE__->table('user_group');
__PACKAGE__->add_columns(qw/user_id group_id/);
__PACKAGE__->belongs_to('users', 'BloonixDB::User', { 'foreign.user_id' => 'self.id' });
__PACKAGE__->belongs_to('groups', 'BloonixDB::Group', { 'foreign.group_id' => 'self.id' });

1;


Nun ist meine Frage, ob die belongs_to() Definition so richtig ist?
Weiß hier jemand auch wie ich die Spalte UNIQUE einbinde?

Viele Grüße,
bloonix
What is a good module? That's hard to say.
What is good code? That's also hard to say.
One man's Thing of Beauty is another's man's Evil Hack.

View full thread DBIx::Class -> has_many und belongs_to