Thread Rose::DB::Object - Relationships (8 answers)
Opened by roooot at 2010-04-02 15:32

roooot
 2010-04-02 15:32
#135632 #135632
User since
2008-03-03
276 Artikel
BenutzerIn
[default_avatar]
Hi Leute,

ich bekomm es einfach nicht auf die Rolle.

Ich will CPAN:Rose::DB::Object als ORM benutzen, ok. Die Pakete mit den Tabellendefinitionen stehen, es fehlen allerdings noch die Relationships.

Ich habe 2 Tabellen: users und users_contact (One-To-One Beziehung)
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
CREATE TABLE IF NOT EXISTS `users` (
`id` smallint(6) NOT NULL auto_increment,
`name` varchar(32) NOT NULL,
`password` varchar(40) NOT NULL,
`password_forcechange` enum('Y','N') NOT NULL,
`last_ip` varchar(15) default NULL,
`last_login` timestamp NULL default NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;


CREATE TABLE IF NOT EXISTS `users_contact` (
`user_id` smallint(6) NOT NULL,
`email` varchar(128) default NULL,
`homepage` varchar(256) default NULL,
`phone` text,
`messenger` text,
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


ALTER TABLE `users_contact`
ADD CONSTRAINT `users_contact_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE;


Wie man sieht, zeigt der Primary_Key von users_contacts (user_id) auf id in der Tabelle users. Ok. PhpMyAdmin erkennt die Beziehung auch.


Nun habe ich meine Tabellenkonfiguration aufgestellt, damit Rose::DB::Object damit arbeiten kann.
Code (perl): (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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package User;

use strict;

use base qw(DB::Object);

__PACKAGE__->meta->setup
(
    table   => 'users',

    columns => 
    [
        id                   => { type => 'integer', not_null => 1 },
        name                 => { type => 'varchar', length => 32, not_null => 1 },
        password             => { type => 'varchar', length => 40, not_null => 1 },
        password_forcechange => { type => 'enum', check_in => [ 'Y', 'N' ], not_null => 1 },
        last_ip              => { type => 'varchar', length => 15 },
        last_login           => { type => 'timestamp' },
    ],

    primary_key_columns => [ 'id' ],

    unique_key => [ 'name' ],    
    
    foreign_keys    =>
    [
    ],
    
    relationships   =>
    [                
        contact     =>
        {          
          type          => 'one to one',
          class         => 'User::Contact',
          column_map    => { id => 'user_id' },
          
        },

    ]

);


1;


package User::Contact;

use strict;

use base qw(DB::Object);

__PACKAGE__->meta->setup
(
    table   => 'users_contact',

    columns => 
    [
        user_id   => { type => 'integer', not_null => 1 },
        email     => { type => 'varchar', length => 128 },
        homepage  => { type => 'varchar', length => 256 },
        phone     => { type => 'text', length => 65535 },
        messenger => { type => 'text', length => 65535 },
    ],

    primary_key_columns => [ 'user_id' ],   
    
    relationships   =>
    [
        user        =>
        {          
          type          => 'one to one',
          class         => 'User',
          column_map    => { 'user_id' => 'id' },
          
        },
    ],
    
);

1;



Rufe ich jetzt
Code (perl): (dl )
 User->new(id=>1)->load->contact->email;
auf, meckert er mich wie folgt an:
Code: (dl )
Can't locate object method "meta" via package "User::Contact" at /usr/share/perl5/Rose/DB/Object/MakeMethods/Generic.pm line 19



Ich habe absolut keine Ahnung mehr was hier los ist. Bin für jede Hilfe dankbar.

Viele Grüße
Viele Grüße :)

View full thread Rose::DB::Object - Relationships