Re: abstract class with Class::DBI

[prev] [thread] [next] [Date index for 2004/10/21]

From: Tom Hukins
Subject: Re: abstract class with Class::DBI
Date: 14:17 on 21 Oct 2004
--mP3DRpeJDSE+ciuQ
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

On Thu, Oct 21, 2004 at 09:00:45AM -0400, William McKee wrote:
> On Wed, Oct 20, 2004 at 05:40:26PM +0100, Tom Hukins wrote:
> > I'm working with a similar schema at the moment, and can attest to
> > "bad things happening" as the documentation claims.
> 
> Were you inflating the primary key or setting up a relationship? What
> bad things were happening?

The attached script demonstrates problems with setting has_a
relationships on a primary key field.  As provided, the script runs
fine, but uncomment the relationship on line 39 and it breaks.

Can you think of an elegant way to specify the relationship that
avoids problems?

As an aside:  I would like to pass the address as an argument to
Person->create instead of having to specify fields inherited through
might_have after creating objects.

Tom

--mP3DRpeJDSE+ciuQ
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline; filename=breakhasa

#!/usr/bin/perl

package MyDB;

BEGIN {
    use base qw(Class::DBI);
    __PACKAGE__->set_db('Main', 'dbi:SQLite:dbname=mytestdb');
    __PACKAGE__->db_Main->do('
	CREATE TABLE person (
	    id          INTEGER PRIMARY KEY,
	    name        VARCHAR(255)
	)
    ');
    __PACKAGE__->db_Main->do('
	CREATE TABLE house (
	    owner       INTEGER PRIMARY KEY,
	    address     VARCHAR(255)
	)
    ');
}


package Person;

use base qw(MyDB);
__PACKAGE__->table(	    'person' );
__PACKAGE__->columns(	    Primary	=> 'id');
__PACKAGE__->columns(	    All		=> qw|name|);
__PACKAGE__->might_have(    house	=> 'House',     qw(address) );
__PACKAGE__->columns(	    Stringify	=> qw|name| );


package House;

use base qw(MyDB);
__PACKAGE__->table(	    'house' );
__PACKAGE__->columns(	    Primary	=> 'owner');
__PACKAGE__->columns(	    All		=> qw|address|);
#__PACKAGE__->has_a(	    owner	=> 'Person' );


package main;

use strict;
use warnings;

my $queen   = Person->create({
	name	    => 'Elizabeth',
});
my $batman  = Person->create({
	name	    => 'Batman',
});
$queen->address('Buckingham Palace');
$batman->address('The Bat Cave');

foreach my $person (Person->retrieve_all) {
    printf "%s lives at %s.\n", $person, $person->address;
}

END {
    unlink 'mytestdb';
}

--mP3DRpeJDSE+ciuQ--

(message missing)

abstract class with Class::DBI
Edward Betts 15:09 on 20 Oct 2004

Re: abstract class with Class::DBI
Tom Hukins 16:40 on 20 Oct 2004

Re: abstract class with Class::DBI
William McKee 13:00 on 21 Oct 2004

Re: abstract class with Class::DBI
Tom Hukins 14:17 on 21 Oct 2004

Re: abstract class with Class::DBI
Perrin Harkins 16:09 on 22 Oct 2004

Re: abstract class with Class::DBI
Tony Bowden 14:41 on 21 Oct 2004

Re: abstract class with Class::DBI
William McKee 18:54 on 21 Oct 2004

Re: abstract class with Class::DBI
Michael 19:17 on 21 Oct 2004

Re: abstract class with Class::DBI
Andreas Fromm 07:00 on 21 Oct 2004

Re: abstract class with Class::DBI
William McKee 19:42 on 21 Oct 2004

Re: abstract class with Class::DBI
Kingsley Kerce 20:01 on 21 Oct 2004

Re: abstract class with Class::DBI
Perrin Harkins 16:14 on 22 Oct 2004

Generated at 11:34 on 01 Dec 2004 by mariachi v0.52