In my Mapper class I'm extending AbstractDbMapper
from ZfcBase
to fetch rows from the database. A simple example would be code like this:
class MyMapper extends AbstractDbMapper
{
//...
public function fetchAll() {
$select = $this->getSelect();
return $this->select($select); // returns HydratingResultSet
}
}
The problem is that $this->select()
returns a Zend\Db\ResultSet\HydratingResultSet
(containing the needed and hydrated objects). But I would like to return an array of these objects instead of a HydratingResultSet
containing the objects.
The first thing to look at would be HydratingResultSet::toArray()
but this returns a multidimensional array instead of an array of objects.
So I chose to do it by hand:
public function fetchAll() {
$select = $this->getSelect();
$results = array();
foreach ($this->select($select) as $object) {
$results[] = $object;
}
return $results; // returns array of needed objects
}
This works but looks ugly in every fetch method. Do I have to modify the code from select() to get the wanted behavior or is there an easier way?
Btw: Is it even recommended to return an array or convert it like this? Thanks for your help!