Zend Framework

Zend_Paginator sets an incorrect lastItemNumber when using the Null adapter.

Details

  • Type: Bug Bug
  • Status: Resolved Resolved
  • Priority: Major Major
  • Resolution: Fixed
  • Affects Version/s: 1.6.0RC1
  • Fix Version/s: 1.6.1
  • Component/s: Zend_Paginator
  • Labels:
    None

Description

When using Zend_Paginator_Adapter_Null, Zend_Paginator::_pages->lastItemNumber is set incorrectly. This is because Zend_Paginator_Adapter_Null::getItems() returns an empty array, which Zend_Paginator::getItemsByPage() casts to an empty ArrayIterator, which, in turn, sets Zend_Paginator::_pages->lastItemNumber to an incorrect number. Shouldn't Zend_Paginator_Adapter_Null::getItems() return an array containing the number of empty values equal to Zend_Paginator_Adapter_Null::_count ? Am I overlooking something?

Here's a patch:

Index: Paginator/Adapter/Null.php
===================================================================
— Paginator/Adapter/Null.php (revision 10727)
+++ Paginator/Adapter/Null.php (working copy)
@@ -58,7 +58,7 @@
*/
public function getItems($offset, $itemCountPerPage)

{ - return array(); + return array_slice(array_fill(0, $this->_count, ''), $offset, $itemCountPerPage); }

/**

Activity

Hide
Matthew Ratzloff added a comment -

Thanks for the excellent bug report, Jim!

Fixed this in revisions 10791-10793.

Show
Matthew Ratzloff added a comment - Thanks for the excellent bug report, Jim! Fixed this in revisions 10791-10793.
Hide
Ken Chou added a comment -

sorry, the bug still here.

return array_slice(array_fill(0, $this->_count, ''), $offset, $itemCountPerPage); work fine.
or better way to do it:
public function getItems($offset, $itemCountPerPage)
{
$remainItemCount = $this->_count > $offset ? $this->_count - $offset : 0;
$currentItemCount = $remainItemCount > $itemCountPerPage ? $itemCountPerPage : $remainItemCount;
return array_fill(0, $currentItemCount, null);
}

Test code:
-------------
$paginator = Zend_Paginator::factory(2);
$paginator->setCurrentPageNumber(1);
$paginator->setItemCountPerPage(5);

$pages = $paginator->getPages();

Zend_Debug::dump($pages);

— except result —
["currentItemCount"] => int(2)
["lastItemNumber"] => int(2)

— actual result —
["currentItemCount"] => int(5)
["lastItemNumber"] => int(5)

Show
Ken Chou added a comment - sorry, the bug still here. return array_slice(array_fill(0, $this->_count, ''), $offset, $itemCountPerPage); work fine. or better way to do it: public function getItems($offset, $itemCountPerPage) { $remainItemCount = $this->_count > $offset ? $this->_count - $offset : 0; $currentItemCount = $remainItemCount > $itemCountPerPage ? $itemCountPerPage : $remainItemCount; return array_fill(0, $currentItemCount, null); } Test code: ------------- $paginator = Zend_Paginator::factory(2); $paginator->setCurrentPageNumber(1); $paginator->setItemCountPerPage(5); $pages = $paginator->getPages(); Zend_Debug::dump($pages); — except result — ["currentItemCount"] => int(2) ["lastItemNumber"] => int(2) — actual result — ["currentItemCount"] => int(5) ["lastItemNumber"] => int(5)
Hide
Ken Chou added a comment -

array_fill cannot accept zeor:

if ($this->_count <= $offset) return array();
$remainItemCount = $this->_count - $offset;
$currentItemCount = $remainItemCount > $itemCountPerPage ? $itemCountPerPage : $remainItemCount;
return array_fill(0, $currentItemCount, null);

Show
Ken Chou added a comment - array_fill cannot accept zeor: if ($this->_count <= $offset) return array(); $remainItemCount = $this->_count - $offset; $currentItemCount = $remainItemCount > $itemCountPerPage ? $itemCountPerPage : $remainItemCount; return array_fill(0, $currentItemCount, null);
Hide
Jurrien Stutterheim added a comment -

Resolved in r17281

Show
Jurrien Stutterheim added a comment - Resolved in r17281

People

Vote (1)
Watch (3)

Dates

  • Created:
    Updated:
    Resolved: