[Dbix-class] Running $rs->count() on full resultset?

Matt S Trout dbix-class at trout.me.uk
Tue Apr 11 14:22:29 CEST 2006


Jesse Sheidlower wrote:
> On Mon, Apr 10, 2006 at 02:52:49PM +0100, Matt S Trout wrote:
>> Jesse Sheidlower wrote:
>>> I'm dynamically constructing my $rs search statements, and including
>>> "page" and "rows" values in my attribute hash, for paged results.
>> Don't do it that way.
>>
>> my $rs = $schema->resultset("CD")->search(...);
>>
>> my $first_page_rs = $rs->page(1);
>>
>> my $total_results = $rs->count;
> 
> I'm afraid this doesn't work.
> 
>   my $paged_rs = $full_rs->page($page); 
> 
> does, indeed, return a rs for the indicated page. But
> 
>   my $total_result_count = $full_rs->count;
> 
> continues to return the number of rows as given by the rows
> attribute in the search statement. Needless to say it gives
> this number regardless of how many rows are returned on a
> given page (since the $total_result_count is derived without
> reference to what page we're looking at).
> 
> Putting it another way, if my full search returns 230 records
> and I'm looking at them 25 at a time, then $total_result_count
> is 25, period. If my full search returns fewer than the number
> given by "rows", I do get the correct number from count().

Shite.

my $pager = $full_rs->pager;

my $total = $pager->total_entries;

Must make 'rows' not mean 'limit' and 'rows per page' at some point 
(presumablty by adding new attributes for both and making rows itself DWIM 
based on context. patches would be welcome :)

-- 
      Matt S Trout       Offering custom development, consultancy and support
   Technical Director    contracts for Catalyst, DBIx::Class and BAST. Contact
Shadowcat Systems Ltd.  mst (at) shadowcatsystems.co.uk for more information

+ Help us build a better perl ORM: http://dbix-class.shadowcatsystems.co.uk/ +



More information about the Dbix-class mailing list