-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Fixes #6812 : Zend\Paginator\Adapter\DbSelect::getItems should return array #6817
Fixes #6812 : Zend\Paginator\Adapter\DbSelect::getItems should return array #6817
Conversation
$items[] = $result; | ||
} | ||
|
||
return $items; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't you avoid to init the result set object at all, and use statement's result directly to fill the final array?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no
Changing returned data from ResultSet to array is a BC. |
should the changed place is in |
There is no need for any BC break and changing of tests. Updating if (!$items instanceof Traversable) {
$items = new ArrayIterator($items);
} If you prepend an if clause to detect if $items is an instance of if ($items instanceof AbstractResultSet) {
$temp = array();
foreach ($items as $key => $value) {
$temp[$key] = $value;
}
$items = $temp;
unset($temp);
} The complete code of /**
* Returns the items for a given page.
*
* @param int $pageNumber
* @return mixed
*/
public function getItemsByPage($pageNumber)
{
$pageNumber = $this->normalizePageNumber($pageNumber);
if ($this->cacheEnabled()) {
$data = static::$cache->getItem($this->_getCacheId($pageNumber));
if ($data) {
return $data;
}
}
$offset = ($pageNumber - 1) * $this->getItemCountPerPage();
$items = $this->adapter->getItems($offset, $this->getItemCountPerPage());
$filter = $this->getFilter();
if ($filter !== null) {
$items = $filter->filter($items);
}
if ($items instanceof AbstractResultSet) {
$temp = array();
foreach ($items as $key => $value) {
$temp[$key] = $value;
}
$items = $temp;
unset($temp);
}
if (!$items instanceof Traversable) {
$items = new ArrayIterator($items);
}
if ($this->cacheEnabled()) {
$cacheId = $this->_getCacheId($pageNumber);
static::$cache->setItem($cacheId, $items);
static::$cache->setTags($cacheId, array($this->_getCacheInternalId()));
}
return $items;
} |
@Martin-P done ;). updated. |
I've added unit test for it. |
@@ -74,7 +74,7 @@ public function __construct(Select $select, $adapterOrSqlObject, ResultSetInterf | |||
* | |||
* @param int $offset Page offset | |||
* @param int $itemCountPerPage Number of items per page | |||
* @return array | |||
* @return ResultSetInterface |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The AdapterInterface#getItems()
defined return type is array
, and it cannot be changed here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DbSelect::getItems()
never returned an array but always returned a ResultSet
. The docs were wrong from the beginning.
See the current master: Zend\Paginator\Adapter\DbSelect
line 90
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd say it is a bug in DbSelect
then, since it does not respect the interface specification.
54d8c11
to
38d9db1
Compare
|
||
$items = array(); | ||
foreach($resultSet as $key => $value) { | ||
$items[$key] = $value; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Urk, this would be a perfect use-case of generators
5402f21
to
8a3721b
Compare
updated loop resultset with |
@@ -86,8 +87,8 @@ public function getItems($offset, $itemCountPerPage) | |||
|
|||
$resultSet = clone $this->resultSetPrototype; | |||
$resultSet->initialize($result); | |||
|
|||
return $resultSet; | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Trailing whitespace here
if it is accepted, then zf2-documentation need to be updated for Album tutorial on loop as $album as object to $album as array.
After
@Ocramius should I ? |
@samsonasik I think |
eb17223
to
1cbd255
Compare
@Ocramius done, I have changed to |
@samsonasik the last change needs a test along the lines of (pseudo-code): $resultset = new Resultset([new ArrayObject(['foo' => 'bar']), new ArrayObject(['foo' => 'bar'])]);
// ...
foreach ($paginator as $item) {
$this->assertInstanceOf('ArrayObject', $item);
} This should prevent a regression, as it will fail with the previous |
@Ocramius I can't reproduce it directly, but I've updated so it looks like : $paginator = new Paginator\Paginator($dbSelect);
$this->assertInstanceOf('ArrayIterator', $paginator->getItemsByPage(1)); and to proof that the paginator items instanceof $paginator = new Paginator\Paginator(new Paginator\Adapter\Iterator($resultSet->getDataSource()));
foreach($paginator as $item) {
$this->assertInstanceOf('ArrayObject', $item);
} please let me know I need to do something, thanks ;) |
@samsonasik that seems to work |
@samsonasik merged, thanks!
|
…inor CS fixes (spacing)
…onstant use over magic constant: see zendframework/zendframework#5505 for reference
…S: whitespace removal
…DbSelect#getItems()` should return `array` types
…ixed `@group` annotations to comply with `DbSelectTest` ones
…dbselect-getitems-should-return-array' into develop Close zendframework/zendframework#6812 Close zendframework/zendframework#6817
Fixes #6812