[CDBI] Best way to do this

William Ross will at spanner.org
Sat Sep 10 14:48:17 BST 2005


On 10 Sep 2005, at 04:27, Kate Yoak wrote:

> I am attempting to do something that ought to be easy, but I can't  
> quite
> figure out the best approach.
>
> BOB->has_many('friends'...);
>
> But I'd like to put a little custom logic into BOB->friend, such as:
>
> sub friends{
>  my $self = shift;
>  my $friends = $self->SUPER::friends(@_);
>  if (@friends ==1 && !wantarray){
>     return shift @friends;
>  }
>  ...
> }
>
> Of course HasMany complains about already having friends defined.   
> I can
> certainly work around this, and I might anyway - but what's a good  
> way to
> solve the general case of doing some post-processing on the data?


BOB->has_many( _friends => ... );

sub friends {
     my $self = shift;
     my $friends = $self->_friends;
     ...
}

note that $friends here will be an iterator.

The other likely alternative is to write a custom retrieval method  
and skip the relationship definition, especially if more fancy SQL is  
required.

best

will





More information about the ClassDBI mailing list