3

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; ?>
GingerHead
  • 8,130
  • 15
  • 59
  • 93
automatix
  • 14,018
  • 26
  • 105
  • 230

1 Answers1

0

Zend\Db\ResultSet\ResultSet or rather Zend\Db\ResultSet\AbstractResultSet is holding the answer to your question. This object has 10-12 methods most of them providing iterative functionality. To answer your question the Zend\Db\ResultSet\ResultSet holds the information retrieved from the Db in its dataSource paramter (Zend\Db\ResultSet\ResultSet::$dataSource).

The paradox of results being iterated and loaded with foreach but not seen by var_dump() can be explained by the fact that the results returned are actually hold in buffered.

If you want to access the result set I suggest you to use Zend\Db\ResultSet\ResultSet::toArray() ($resultSet->toArray()). This will return an array of arrays where each array is a row in the DB table.

Hope this helps, feedback will be appreciated :),

Stoyan.

Stoyan Dimov
  • 5,250
  • 2
  • 28
  • 44
  • Thank you for your answer! But `Zend\Db\ResultSet\ResultSet#toArray()` doesn't work for me. I called it (`$courses->toArray();`) and set a breakpoint to on the [first line](https://github.com/zendframework/zf2/blob/master/library/Zend/Db/ResultSet/AbstractResultSet.php#L256) of `Zend\Db\ResultSet\AbstractResultSet#toArray()`. Then I reload the page and expeted the code -- the property `$dataSource` was empty. And an exception was thrown: `Rows as part of this DataSource, with type object cannot be cast to an array`. – automatix Apr 02 '13 at 09:56
  • Without the breakpoint, what did the `$courses->toArray();` returned? – Stoyan Dimov Apr 02 '13 at 10:25
  • 3
    It returns this exception: `Rows as part of this DataSource, with type object cannot be cast to an array` – automatix Apr 03 '13 at 23:47