Re: Pagers and Iterators
[prev]
[thread]
[next]
[Date index for 2005/02/25]
On 25 Feb 2005, at 13:23, Carl Johnstone wrote:
>> Not really. The munging is actually a live calculation. Sorry, I
>> should
>> really have been much clearer when I simplified the example.
>>
>> The easiest thing I could think of would be to create a new
>> Class::DBI::Iterator but the new method of that does not look designed
>> to be called manually.
>
> If you need to calculate something for each row to be able to sort,
> you're not going to save yourself anything by using an iterator -
> you'll already have created objects for every row in the DB.
that's true, but with some care it won't cost much extra either, and it
has the useful consequence that you can use a single list-sorting
component at the template level rather than maintaining lots of
separate-but-similar sort operations in your data classes.
it's easy to build an iterator:
my $it = Class::DBI::Iterator->new( $object_class, [
object_or_id_list ], @mappers );
where @mappers is a list of method names that you want called on each
object as it is pulled from the list. You can also dip into cdbi's
normal processes a bit earlier:
my $it = $object_class->_ids_to_objects( [object_or_id_list] );
but it's _private and to my eyes less easily understood, since they're
not ids at all.
I don't think this is a particularly expensive thing to do. I'd like to
see a tweak to CDBI::_init so that if its $data hashref is already
blessed into the right class it just returns it, but the object stash
accomplishes the same thing once it has calculated the object's unique
id.
best
will
> As far as getting your paging working correctly, use Data::Page
> directly:
>
> my $page = Data::Page->new($total_entries, $entries_per_page,
> $current_page);
>
> my @sorted = sort { $a->foo cmp $b->foo } Class->retrieve_all
> my @wanted = $page->splice(\@sorted);
>
>
>
> Carl
>
|
(message missing)
|