In a Zend Framework application, that is built pretty similar to the album application from the ZF2 Getting Started tutorial, I use a ResultSet object to transport the data from the model over the controller to the view (for details see the code below).
This works fine, but I don't get, where the ResultSet
object holds the data. I can loop it e.g. with foreach
and get the data row byrow (or better model object by model object). But when I debug it in my IDE or simply with var_dump(...)
, it seems to be empty.
How/where does a Zend\Db\ResultSet\ResultSet
object hold the data, retrieved from the database?
The relevant parts of the code:
Module settings:
class Module implements ConfigProviderInterface, ServiceProviderInterface, AutoloaderProviderInterface {
...
public function getServiceConfig() {
try {
return array (
'factories' =>array(
...
'SportTable' => function ($serviceManager) {
$tableGateway = $serviceManager->get('SportTableGateway');
$table = new SportTable($tableGateway);
return $table;
},
'SportTableGateway' => function ($serviceManager) {
$dbAdapter = $serviceManager->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Sport());
return new TableGateway('sports', $dbAdapter, null, $resultSetPrototype);
},
...
)
);
}
...
}
Model (table):
class CourseTable {
...
public function findAllByCityNameAndSportTitle($cityName, $sportTitle) {
$select = new Select();
$where = new Where();
...
$resultSet = $this->tableGateway->selectWith($select);
return $resultSet;
}
...
}
Model (mapper):
class Course implements ArraySerializableInterface {
public $id;
...
public function exchangeArray(array $data) {
$this->id = (isset($data['id'])) ? $data['id'] : null;
...
}
...
}
Controller:
class CatalogController extends AbstractActionController {
...
public function listCoursesAction() {
$cityName = $this->params()->fromRoute('city', null);
$sportTitle = $this->params()->fromRoute('sport', null);
return new ViewModel(array(
'city' => $cityName,
'sport' => $sportTitle,
'courses' => $this->getCourseTable()->findAllByCityNameAndSportTitle($cityName, $sportTitle)
));
}
...
}
View:
<?php foreach ($courses as $course) : ?>
<div>
<div>id: <?php echo $this->escapeHtml($course->id); ?></div>
...
</div>
<?php endforeach; ?>