Object Index

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

From: Jason Galea
Subject: Object Index
Date: 00:19 on 19 Apr 2005
This is a multi-part message in MIME format.
--------------040102080805040701080504
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

Hi all,

I think I've managed to break the object index but I've been staring at 
this waaay too long.. (I started with a lot more code with a rather 
subtle bug..)

If you create an object (which has an auto_increment id), then retrieve 
another copy of that object and change a field value, your original 
object won't get the changes.

I think what is happening is that the object index won't index the newly 
created item as it doesn't have it's id. When the copy is retrieved it 
is indexed but the original object has no connection to the indexed object.

Modifying the _create method in Class/DBI.pm to call _init before 
returning appears to fix the problem but I can't see it being the best 
solution.

cheers,

J

--------------040102080805040701080504
Content-Type: application/x-troff;
 name="test.t"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="test.t"


use Test::More tests => 3;

use lib './';

use Product;

Product->connection('dbi:mysql:test','','', { AutoCommit => 1 });

eval{ Product->db_Main->do( 'drop table product' ); };
Product->db_Main->do( Product->sql_create_table );


print "# create a product with available stock = 5\n";
my $product = Product->create({ title => 'Phantom Of The Opera, The (2004)',
                                available_stock => 5 });
print "# product id = ".$product->id."\n";

ok ( $product->available_stock == 5, 'available stock = 5' );

my $copy = Product->retrieve(1);
$copy->available_stock(4);
$copy->update;

ok ($copy->available_stock == 4, 'copy->available_stock = 4' ) or print "# got: ".$copy->available_stock."\n";
ok ($product->available_stock == 4, 'product->available_stock = 4' ) or print "# got: ".$product->available_stock."\n";


--------------040102080805040701080504
Content-Type: application/x-perl;
 name="Product.pm"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="Product.pm"

package Product;
use base 'Class::DBI';

__PACKAGE__->table('product');

__PACKAGE__->columns( Primary   => 'id'     );
__PACKAGE__->columns( All       => qw/ id title available_stock / );

sub sql_create_table{
  return <<_EOP_;
    CREATE TABLE if not exists product(
      id               int auto_increment,
      title            varchar(32),
      available_stock  int not null,
      primary key (id)
    );
_EOP_
}

1;


--------------040102080805040701080504--

Object Index
Jason Galea 00:19 on 19 Apr 2005

ContextualFetch 1.02 steps on $DBI::errstr
Rod McChesney 02:40 on 19 Apr 2005

Re: Object Index
Perrin Harkins 03:19 on 19 Apr 2005

Re: Object Index
Jason Galea 07:02 on 19 Apr 2005

Generated at 09:29 on 27 Apr 2005 by mariachi v0.52