Skip to content

Instantly share code, notes, and snippets.

@newage
Last active June 30, 2016 11:43
Show Gist options
  • Save newage/5b4739a8f5a0c151ec9581fac2ba23e9 to your computer and use it in GitHub Desktop.
Save newage/5b4739a8f5a0c151ec9581fac2ba23e9 to your computer and use it in GitHub Desktop.
Zend Frameword 2 create a table factory
namespace Album;
// Add these import statements:
use Album\Model\Album;
use Album\Model\AlbumTable;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;
class Module
{
// getAutoloaderConfig() and getConfig() methods here
// Add this method:
public function getServiceConfig()
{
return array(
'factories' => array(
'Users\Model\HelloTable' => function($sm) {
$tableGateway = $sm->get('AlbumTableGateway');
$table = new HelloTable($tableGateway);
return $table;
},
'AlbumTableGateway' => function ($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Album());
return new TableGateway('album', $dbAdapter, null, $resultSetPrototype);
},
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment