On 3/29/07 6:27 AM, James Masters wrote:
> So now $products is an array of all product information, just
> like %product was a hash of all product info. But how do I
> elegantly get that stock quantity info for a particular location
> out of the $products array? I need something like:
>
> foreach $prod (@$products) {
> $prod->locations->$location->stockqty
> or
> $prod->locations(locationcode => $location)->stockqty
> }
>
> Is there a oneliner solution? Or do I just have to do another call to the DB
> or simply iterate through locations?
You need to loop, because objects related through a one-to-many relationship
are stored as (and are, in fact) a list, not a keyed collection of unique
entities. If you really want to do it in a single line, you can create a
convenience method to encapsulate the loop:
My::DB::Product;
...
use Carp;
...
sub location_by_code
{
my($self, $code) = @_;
my @locs = grep { $_->locationcode eq $code } $self->locations;
croak "Found more than one location with code '$code'" if(@locs > 1);
return $locs[0];
}
and then:
$qty = $product->location_by_code($loc_code)->stockqty;
-John
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Rose-db-object mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/rose-db-object