Problem with empty has_a

[prev] [thread] [next] [Date index for 2005/05/19]

From: Thomas Klausner
Subject: Problem with empty has_a
Date: 19:34 on 19 May 2005
--pf9I7BMVVzbSWLtt
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

Hi!

I've got a problem and I'm not sure whether it's a bug, a (documented?)
feature or a failure on my part.

I define two classes, where one class has_a relationship to the other class.
But if on object on the first class doesn't actually is linked to the other
class, the accessor returns an object with id=0 that does not exist in the DB.

As my explanation might not be to clear, I attached some code to illustrate
the problem. The code creates a sqlite db file called domm_test.db and
deletes it afterwards, so don't run the script if you have a file with this
name in currentdir :-)

Anyway, what's causing the problem and what can I do to get rid of it.

Oh, if you need more info on whats going on add
  use Data::Dumper;
  print Dumper $bar->foo;
at line 42 (coincidence, really!)


        -- 
        #!/usr/bin/perl                               http://domm.zsi.at
for(ref bless{},just'another'perl'hacker){s-:+-$"-g&&print$_.$/}

--pf9I7BMVVzbSWLtt
Content-Type: text/x-perl; charset=us-ascii
Content-Disposition: attachment; filename="cdbi_hasa_problem.pl"

#!/usr/bin/perl

use strict;
use warnings;
use DBI;

my $db='domm_test.db';

# set up DB
unlink($db) if -e $db;
my $DBH=DBI->connect("dbi:SQLite:dbname=$db");
$DBH->do('create table foo (id,foo)');
$DBH->do('create table bar (id,bar,foo)');
$DBH->do('insert into foo values (?,?)',undef,1,'foo');
$DBH->do('insert into bar values (?,?,?)',undef,1,'bar',1);
$DBH->do('insert into bar values (?,?,?)',undef,2,'bar',0);

use Class::DBI;

# define classes
package MyBase;
use base 'Class::DBI';
MyBase->connection("dbi:SQLite:dbname=$db");

package Foo;
use base 'MyBase';
Foo->table('foo');
Foo->columns(All=>qw[id foo]);

package Bar;
use base 'MyBase';
Bar->table('bar');
Bar->columns(All=>qw[id bar foo]);
Bar->has_a(foo=>'Foo');

# problem!
package main;

foreach (1,2) {
    my $bar=Bar->retrieve($_);
    next unless defined $bar->foo;
    print "$_ has Foo: ".$bar->foo->foo."\n";
}


--pf9I7BMVVzbSWLtt--

Problem with empty has_a
Thomas Klausner 19:34 on 19 May 2005

Re: Problem with empty has_a
Tony Bowden 21:01 on 19 May 2005

Re: Problem with empty has_a
Thomas Klausner 07:05 on 20 May 2005

Re: Problem with empty has_a
Thomas Klausner 07:39 on 20 May 2005

Re: Problem with empty has_a
merlyn (Randal L. Schwartz) 12:20 on 20 May 2005

Re: Problem with empty has_a
Tony Bowden 12:33 on 20 May 2005

Re: Problem with empty has_a
Perrin Harkins 13:43 on 20 May 2005

Generated at 15:51 on 25 May 2005 by mariachi v0.52