Build an iterator for getting all products as objects, one at a time.

Build tests for the iterator.
Begin working on the exporter (which will use the iterator).
This commit is contained in:
Colin Kuskie 2008-05-28 04:37:21 +00:00
parent ab0611d2a1
commit 0a05d637d4
3 changed files with 103 additions and 1 deletions

View file

@ -272,6 +272,38 @@ sub getAllCollateral {
}
#-------------------------------------------------------------------
=head2 getAllProducts ( $session )
A class method to return an iterator for getting all Products (and Product sub-classes)
as Asset objects, one at a time. The iterator will return undef when you've reached
the end of the list of products.
It should be used like this:
my $productIterator = WebGUI::Asset::Product->getAllProducts($session);
while (my $product = $productIterator->()) {
##Do something useful with $product
}
=cut
sub getAllProducts {
my $class = shift;
my $session = shift;
my $sth = $session->db->read('select distinct(assetId) from Product');
return sub {
my ($assetId) = $sth->array;
if (!$assetId) {
$sth->finish;
return undef;
}
return WebGUI::Asset->newPending($session, $assetId);
}
}
#-------------------------------------------------------------------
=head2 getCollateral ( tableName, keyName, keyValue )

View file

@ -39,7 +39,8 @@ file will be named siteProductData.csv.
sub exportProducts {
my $session = shift;
my $productData = '';
my @columns = qw{sku title shortdescription price weight quantity};
my $productData = WebGUI::Text::joinCSV('mastersku', @columns) . "\n";
my $storage = WebGUI::Storage->createTemp($session);
$storage->addFileFromScalar('siteProductData.csv', $productData);
return $storage;