Dat Tran
Dat Tran

Reputation: 1586

Many table gateway in a module zf2

I'm building an CMS with zend framework 2. I have 2 questions and hope you can help me.

  1. My module uses many different SQL tables. Following the example in skeleton-application, I have to do like this:

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. I want to ask when we use "fatories" in getServiceConfig, is it true that the model will be initialized only when we call it? Thank you so much for your reply!

Upvotes: 0

Views: 391

Answers (1)

blackbishop
blackbishop

Reputation: 32660

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');

Upvotes: 1

Related Questions