I'm using Yii and yii mongoDb extension (YMDS).
I have a case when ->count()
return correct number of result but ->findAll()
no.
// calculate total numbers of document
Document::model()->setUseCursor(true);
$total = Document::model()->count($criteria);
Document::model()->setUseCursor(false);
$criteria->sort($sort_by, $direction);
$criteria->limit($limit);
$criteria->offset($page);
if (!($docs = Document::model()->findAll($criteria))) {
throw new CHttpException(404, 'can not find docs');
}
So in $total
I see number 2, but $docs
has only 1, what could be the reason ?
The issue is that you have misunderstood the offset()
parameter:
$criteria->offset($page);
Offset skips the given number of results, so if $page is 1 (as per OP's comment), the first result will be skipped.
In actual use for pagination you want the offset to be something like:
$criteria->offset($entries_per_page * $current_page_number)
$current_page_number should be 0-indexed so if your $entries_per_page is 20 the offset for display pages would be:
If you're using Zii widgets like CListView or CGridView they inherit from Yii's CPagination pager class which takes care of offset calculations and more.