In Zf2 application written the model file to retrieve the data set from the table, it works as expected for returning one result set, but for returning multiple rows not able to achieve by the below code.
Working and Returning single row
/**
* @param $id
* @return bool|Entity\Feeds
*/
public function getAppFeed($id)
{
$row = $this->select(array('app_id' => (int)$id))->current();
if (!$row)
return false;
$feedVal = new Entity\Feeds(array(
'id' => $row->id,
'title' => $row->title,
'link' => $row->link,
'Description' => $row->description,
'created' => $row->created,
));
return $feedVal;
}
Removed current and tried tablegateway object also but throwing the error.
Feeds table will have multiple record for each of the application, I need a function to achieve the same.
The Select
always returns a ResultSet
. You can access the objects(1) of ResultSet by iterating over it, because it implements the Iterator
Interface.
Just an example piece of code:
public function getAppFeed($id)
{
$resultSet = $this->select(array('app_id' => (int)$id));
if ($resultSet instanceof \Zend\Db\ResultSet) {
foreach($resultSet as $item) {
// do your feed stuff here
// e.g. $item->id
}
} else {
return false;
}
}
(1) Object: meaning whatever object you asigned as Prototype
in your TableGateway
.
For further details, please checkout the documentation of ResultSet.