Search code examples
zend-framework2zend-dbtablegateway

(Why) Is Zend\Db\ResultSet a TableGateway Feature?


The Zend Framework 2 documentation for the Zend\Db\TableGateway\TableGateway says, that:

The [Zend\Db\TableGateway\TableGateway] constructor can take Features in 3 different forms: as a single feature object, as a FeatureSet object, or as an array of Feature objects.

And TableGateway constructor actually checks the type.

So, the fourth argument of the TableGateway constructor needs to be compatible to Feature\AbstractFeature or Feature\FeatureSet or to be an array of Feature\AbstractFeature compatible objects.

In the model part of the Get Started tutorial an object of type TableGateway is created and it gets a Zend\Db\ResultSet\ResultSet as fourth argument:

class Module
{
    // getAutoloaderConfig() and getConfig() methods here

    // Add this method:
    public function getServiceConfig()
    {
        return array(
            'factories' => array(
                ...
                'AlbumTableGateway' => function ($sm) {
                    $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                    $resultSetPrototype = new ResultSet();
                    $resultSetPrototype->setArrayObjectPrototype(new Album());
                    return new TableGateway('album', $dbAdapter, null, $resultSetPrototype);
                },
            ),
        );
    }
}

ResultSet is not an instanceof AbstractFeature. But it works.

How does it work?


Solution

  • The $feature is the third argument, see TableGateway#31

    The fourth parameter needs to be compatible with the ResultSetInterface