I'm building an CMS with zend framework 2. I have 2 questions and hope you can help me.
public function getServiceConfig() {
return array(
'factories' => array(
'Album\Model\AlbumTable' => function($sm) {}, 'AlbumTableGateway' => function ($sm) {}, 'Album\Model\Trackable' => function($sm) {}, 'TrackTableGateway' => function ($sm) {}, 'Album\Model\ArtistTable' => function($sm) {}, 'ArtistTableGateway' => function ($sm) {}, 'TrackTableGateway' => function ($sm) {}, 'Album\Model\SingerArtistTable' => function($sm) {}, 'SingerTableGateway' => function ($sm) {}, ... ), ); }
So should I put many models in gServiceConfig() like above? Or can you suggest me any other patterns?
1) You can avoid that redundant code of creating factories for each Table Class. You'll just have something like this :
'invokables'=>array(
'ModuleName\Model\TableA' => 'ModuleName\Model\TableA',
'ModuleName\Model\TableB' => 'ModuleName\Model\TableB',
'ModuleName\Model\TableC' => 'ModuleName\Model\TableC',
),
For that, you can follow this intersting post : Setting Default Db Adapter.
2) Yes, The service manager doesn't make an instance of anything until you request it, i.e. the instance is created only when you call, for example :
$this->getServiceLocator()->get('Trackable');