Re: Possible imrovement to Class::DBI::retrieve

[prev] [thread] [next] [Date index for 2005/06/10]

From: William Ross
Subject: Re: Possible imrovement to Class::DBI::retrieve
Date: 09:26 on 10 Jun 2005
On 10 Jun 2005, at 03:10, leif.eriksen@xxx.xxx.xx wrote:

> <snip>

> package Item;
> ...
> use base qw(Project::DBI);
>
> __PACKAGE__->table('Item');
> __PACKAGE__->columns(All => qw(Id Area Box...));
> __PACKAGE__->has_a(Box => 'Project::Box');
> __PACKAGE__->add_constraint('CheckArea', Area => \&check_area);
> ...
> sub check_area {
>   # check that the Item Area == Box Area
>   my ($area, $self, $column, $others) = @_;
>   ...
>   my $box = $self->_retrieve_box($others);
>   return $area == $box->Area() ? 1 : 0;
> }
>
> sub _retrieve_box {
>   my ($self, $others) = @_;
>
>   my $box;
>
>   if (ref($self)) { # changing an existing Item
>     if (defined $self->Box()) {
>       $box = Box->retrieve($self->Box());
>     }
>   } else { # creating a new tag
>     if (exists $others->{box}) { # a Box was specified in the create
>       $box = Box->retrieve($others->{box});
>     }
>   }
>
>   return $box;
> }
>
> <snip>

> my $item = Item->create({Id=>1, Area=>'Vic', Box=>$box}); #$box->isa 
> ('Box'), not a simple scalar
> ...
>
> This code fails in Class::DBI::retrieve (v0.96)

This would normally just work, but I think it throws an error when  
your _retrieve_box method calls:

>       $box = Box->retrieve($others->{box});

when you're creating a new Item. I don't exactly follow what you're  
trying to do there: you've already supplied a box parameter to  
create, then you retrieve it again in a constraint, but don't check  
it... odd. But perhaps there's more to it than you've shown here.

Anyway, if you really need the constraint checking the easiest thing  
will be to change your code so that it only inflates $others->{box}  
if it isn't already inflated.

best

will

Possible imrovement to Class::DBI::retrieve
leif.eriksen 02:10 on 10 Jun 2005

Re: Possible imrovement to Class::DBI::retrieve
William Ross 09:26 on 10 Jun 2005

Generated at 16:36 on 28 Jul 2005 by mariachi v0.52