Re: [CDBI] Many to many help

[prev] [thread] [next] [Date index for 2005/08/01]

From: Bill Moseley
Subject: Re: [CDBI] Many to many help
Date: 16:42 on 01 Aug 2005
On Mon, Aug 01, 2005 at 10:38:05AM +0200, Mike McKay wrote:
> PassengerRouteJoin->has_a(passenger => "Passenger");
> PassengerRouteJoin->has_a(route => "Route");
> 
> Route->has_many(passengers => [ "PassengerRouteJoin" => 'passenger' ]);
> Passenger->has_many(routes => [ "PassengerRouteJoin" => 'route' ]);
> 
> So then I try and do this:
> 
> my $p = Passenger->create({surname => "Blah", first_name => "Foo"}); 
> 
> my $r = Route->create({departure_point => "Lilongwe", destination => 
> "Likoma"});
> 
> $p->add_to_routes({route => $r});
> 
> Can't deflate route: 8 is not a Route at 
> /usr/local/share/perl/5.8.4/Class/DBI/Relationship/HasA.pm line 87

Odd, this seems to work fine for me.  I think I just copied your
example:


my $p = Passenger->create({surname => "Blah", first_name => "Foo"});
my $r = Route->create({departure_point => "Lilongwe", destination => "Likoma"});

$p->add_to_routes({route => $r});

my @links = PassengerRouteJoin->retrieve_all;
use Data::Dumper; print Dumper \@links;

And I see:

$VAR1 = [
          bless( {
                   'passenger' => bless( {
                                           'id' => '1'
                                         }, 'Passenger' ),
                   'route' => bless( {
                                       'id' => '1'
                                     }, 'Route' )
                 }, 'PassengerRouteJoin' )
        ];

Here's my complete test script for postgres:

--- travel.pl ----

package DB;
use base 'Class::DBI';
DB->connection('dbi:Pg:dbname=travel', 'moseley', '');


package Passenger;
use base 'DB';
__PACKAGE__->table('passenger');
__PACKAGE__->columns(All => qw/id surname first_name/);
__PACKAGE__->sequence( 'passenger_id_seq' );

package Route;
use base 'DB';
__PACKAGE__->table('route');
__PACKAGE__->columns(All => qw/id departure_point destination/);
__PACKAGE__->sequence( 'route_id_seq' );

package PassengerRouteJoin;
use base 'DB';
__PACKAGE__->table('passenger_route_join');
__PACKAGE__->columns(All => qw/passenger route/);
__PACKAGE__->columns(Primary => qw/passenger route/);



package main;
use strict;
use warnings;


DB->db_Main->do("DROP TABLE $_") for qw/passenger_route_join passenger route/;
DB->db_Main->do(<<EOF);
    create table passenger (
        id              SERIAL PRIMARY KEY,
        surname         text,
        first_name      text
    );

    create table route (
        id              SERIAL PRIMARY KEY,
        departure_point text,
        destination     text
    );

    create table passenger_route_join (
        passenger       integer NOT NULL REFERENCES passenger,
        route           integer NOT NULL REFERENCES route,
        PRIMARY KEY (passenger, route)
    );
EOF


PassengerRouteJoin->has_a(passenger => "Passenger");
PassengerRouteJoin->has_a(route => "Route");

Route->has_many(passengers => [ "PassengerRouteJoin" => 'passenger' ]);
Passenger->has_many(routes => [ "PassengerRouteJoin" => 'route' ]);


my $p = Passenger->create({surname => "Blah", first_name => "Foo"});
my $r = Route->create({departure_point => "Lilongwe", destination => "Likoma"});

$p->add_to_routes({route => $r});

my @links = PassengerRouteJoin->retrieve_all;
use Data::Dumper; print Dumper \@links;

$p->dbi_commit;




        -- 
        Bill Moseley
moseley@xxxx.xxx


_______________________________________________
ClassDBI mailing list
ClassDBI@xxxxx.xxxxxxxxxxxxxxxx.xxx
http://lists.digitalcraftsmen.net/mailman/listinfo/classdbi

[CDBI] Many to many help
Mike McKay 08:38 on 01 Aug 2005

Re: [CDBI] Many to many help
William Ross 09:16 on 01 Aug 2005

Re: [CDBI] Many to many help
William Ross 11:03 on 01 Aug 2005

Re: [CDBI] Many to many help
Bill Moseley 16:42 on 01 Aug 2005

Generated at 21:01 on 07 Aug 2005 by mariachi v0.52