Re: abstract class with Class::DBI
[prev]
[thread]
[next]
[Date index for 2004/10/21]
--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)
|